简体   繁体   中英

NSDateFormatter string

What is the NSDateFormatter string for the following date/Time : 02/22/2011 10:43:00 AM

Basically what I wanted was to read a string 02/22/2011 19:00:23 PM , convert it to NSDate and then print is appropriately. My previous attempt:

NSDateFormatter* dtFormatter = [[NSDateFormatter alloc] init];
[dtFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss a"];

NSDate* dt3 = [dtFormatter dateFromString:@"02/22/2011 19:00:23 PM"];
NSString* strDate3 = [dtFormatter stringFromDate:dt3];
NSLog(@"Third = %@",strDate3);

Every time I tried that it would print NULL, it worked when I removed the trailing PM.

Finally, I added the following lines right after the 2nd line in the code above:

[dtFormatter setPMSymbol:@"PM"];
[dtFormatter setAMSymbol:@"AM"];
MM/dd/Y hh:mm:ss a

更多信息请参阅: http//unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

Try it this will give you the required format..

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy HH:mm:ss aaa"];

// Conversion of NSString to NSDate
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [formatter dateFromString:curDate];   //give what u want to give curDate
[formatter release];

// Conversion of NSDate to NSString
NSString *dateString = [formatter stringFromDate:datePicker.date]; //give date here
[formatter release];

This is the correct way if you want string from NSDate object...Now try it

Good Luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM