NSDateFormatter String

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

Basically I wanted to read line 02/22/2011 19:00:23 PM , convert it to NSDate , and then print accordingly. 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 for it to print NULL, it worked when I deleted the final PM.

Finally, I added the following lines immediately after the second line in the code above:

 [dtFormatter setPMSymbol:@"PM"]; [dtFormatter setAMSymbol:@"AM"]; 
+4
source share
2 answers
 MM/dd/Y hh:mm:ss a 

Fore more details see: http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns

+2
source

Try it, it will give you the format you need.

 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 need a string from an NSDate object ... Now try

Good luck

+2
source

Source: https://habr.com/ru/post/1340794/


All Articles