If we need to use the current time as a string as the name of a picture, we need to convert the current time NSDate type data into the NSString type.
or in the network request, our time on the network is a string but locally we need to output the nsdate type of data.
So providing two methods the first is to convert data of type nsdate to nsstring type data, the second is to convert nsstring type data into NSDate type data
1.NSDate-NSString
// Get current Time NSDate *date = [NSDate date]; // Set Date format nsdateformatter* formatter1 = [[NSDateFormatter alloc] init];[ Formatter1 Setdateformat:@ "yyyymmddhhmmss"]; // becomes a number nsstring* str = [Formatter1 stringfromdate:date]; NSLog (@ "datestring =%@", str);
2.NSString-NSDate
//Time StringNSString *str =@"20150806070733";//Specified time formatnsdateformatter* formatter =[[NSDateFormatter alloc] init]; [Formatter Setdateformat:@"YYYYMMDDHHMMSS"];//Set time zone global standard Time cut must set we want to set the time zone of ChinaNstimezone *zone = [[Nstimezone alloc] initwithname:@ "CUT"];[Formatter settimezone:zone];//change back to date formatNSDate *stringdate =[Formatter datefromstring:str]; NSLog (@"stringdate =%@", stringdate);
Conversion between IOS NSDate and NSString