简体   繁体   中英

set date format dynamically using NSDateFormatter

I am getting date and time from server from that i need to get date.

sometimes "2015-04-20 11:40:05 Etc/GMT"and sometimes "2015-04-20T11:40:05Z"

so how to set date format dynamically?

below is my code:

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *purchaseDate = [dateFormatter dateFromString:strPurchaseDate];

I want to set date format dynamically. Any Help?

您可以尝试使用NSDataDetector http://nshipster.com/nsdatadetector/

Consider using NSDateFormatter dateFormatFromTemplate:options:locale: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDateFormatter_Class/Reference/Reference.html#//apple_ref/occ/clm/NSDateFormatter/dateFormatFromTemplate:options:locale :

eg with current locale value ([NSLocale curentLocale]) or use [dateFormatter setLocale:...], specifying selected language

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

and [dateFormatter setTimeStyle:...] to set it t NSDateFormatterShortStyle , NSDateFormatterMediumStyle , or NSDateFormatterLongStyle

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