简体   繁体   中英

NSDateFormatter with T-Z Time format

I got a string in TZ format such as "2015-12-12T05:30:03.634Z" from Google server. How can I convert it to format "YYYY-MM-dd HH:mm:ss" by NSDateFormat ?

I've tried some codes below, but always got (null) in variable dte.

+(NSDate*)tzStrToDateTime:(NSString*)tzStr{
  NSLog(@"Convert tzStr: %@ ",tzStr);
  NSString *tzFormat = @"yyyy-MM-dd'T'HH:mm:ss.SSSX";//2015-12-12T05:30:03.634Z
  NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
  [dateFormat setDateFormat:tzFormat];
  [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Taipei"]];
  NSDate *dte = [dateFormat dateFromString:tzStr];
  NSLog(@"to Date: %@", dte);
  return dte;
}

initial dateFormat is like

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat: @"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Taipei"]];
NSDate *dte = [dateFormat dateFromString:@"2015-12-12T05:30:03.634Z"];
//Second Conversion
 [dateFormat setDateFormat: @"yyyy-MM-dd HH:mm:ss"];
 NSString *finalStr = [dateFormat StringFromdate:dte];

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