I want date in below format.
04/22/2011 1:05:21 PM
format is mm/dd/yyyy time(12 hour) pm or AM
what is the code for this?
Thread SafetyOn iOS 7 and later NSDateFormatter is thread safe. In macOS 10.9 and later NSDateFormatter is thread safe so long as you are using the modern behavior in a 64-bit app.
"en_US_POSIX" is also invariant in time (if the US, at some point in the future, changes the way it formats dates, "en_US" will change to reflect the new behaviour, but "en_US_POSIX" will not), and between machines ("en_US_POSIX" works the same on iOS as it does on OS X, and as it it does on other platforms).
The NSDate class provides methods for comparing dates, calculating the time interval between two dates, and creating a new date from a time interval relative to another date.
Press CTRL+1. In the Format Cells box, click the Number tab. In the Category list, click Date, and then choose a date format you want in Type.
NSDate *date=[NSDate date];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/dd/yyyy hh:mm:ss a"];
NSString *dateOfGame =[formatter stringFromDate:date];
NSLog(@"dateOfGame%@",dateOfGame);
[formatter release];
Check NSDateFormatter (and Date Formatting)
MM/dd/yyyy hh:mm:ss a
some other type
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"MM/dd/yyyy"];
NSLog(@"date=%@",[formatter stringFromDate:date]);//date=04/22/2011
[formatter setTimeStyle:NSDateFormatterBehavior10_4];
NSLog(@"date=%@",[formatter stringFromDate:date]);// date=1:05:11 PM
[formatter setTimeStyle:NSDateFormatterBehaviorDefault];
NSLog(@"date=%@",[formatter stringFromDate:date]); date=
[formatter setTimeStyle:NSDateFormatterFullStyle];
NSLog(@"date=%@",[formatter stringFromDate:date]); date=1:05:11 PM India Standard Time
[formatter setTimeStyle:NSDateFormatterLongStyle];
NSLog(@"date=%@",[formatter stringFromDate:date]); date=1:05:11 PM GMT+05:30
[formatter setTimeStyle:NSDateFormatterMediumStyle];
NSLog(@"date=%@",[formatter stringFromDate:date]); date=1:05:11 PM
[formatter setTimeStyle:NSDateFormatterNoStyle];
NSLog(@"date=%@",[formatter stringFromDate:date]);date=
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(@"date=%@",[formatter stringFromDate:date]); date=1:05 PM
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With