Len Len - 3 months ago 7
Objective-C Question

Converting a date formatted string to be used as a string

I have this code

NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSLog(@"%@",[dateFormatter stringFromDate:[NSDate date]]);


Which gives an output like this: 20160815085427

I would like to use that output as the string 20160815085427
something like this

myDateNumberString = thatString

How can I do that?

Answer

Just use the same expression you are using in NSLog in a variable.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSString *myDateNumberString = [dateFormatter stringFromDate:[NSDa‌​te date]];
NSLog(@"%@", myDateNumberString);
Comments