Convert NSString Date to NSDate

Is there an efficient way to convert from this NSString to NSDate data type:

2011-05-07 + 02: 00

0
source share
5 answers

To do this, you should use the NSDateFromatter class. Set your date format, then use

- (NSDate *)dateFromString:(NSString *)string 

method. See the NSDateFormatter link for more details .

+1
source

It is much better to use NSDateFormatter . You can specify the string format, locale, time zone.

 dateFormatter = [[NSDateFormatter alloc] init]; NSLocale *en_US = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; [dateFormatter setLocale:en_US]; [en_US release]; [dateFormatter setDateFormat:@"MM/dd/yyyy h:mm:ss a"]; [dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:-60*60*7]]; NSDate *date = [dateFormatter dateFromString:dateString]; [dateFormatter release]; 
+1
source

As ssteinberg said, and the format is as follows.

 // 2011-05-07+02:00 [dateFormatter setDateFormat:@"yyyy-MM-dd+hh:mm:"]; 
0
source

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


All Articles