iOS日期格式2016年3月28日/下午16:54

时间:2017-05-15 11:12:14

标签: ios objective-c nsdateformatter

我想以字符串“2017-05-05T08:27:31.337Z”以“2016年3月28日/下午16:54”的格式显示日期。我怎样才能做到这一点? iOS有什么buildin方法吗?感谢。

2 个答案:

答案 0 :(得分:1)

使用NSDateFormatter作为您的概念

例如

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *msgDate = [dateFormatter dateFromString:@"2017-05-05T08:27:31.337Z"];
 [dateFormatter setDateFormat:@"dd MMM yyyy / HH:mma"];
NSString *final = [dateFormatter stringFromDate:msgDate];

<强>更新

setAMSymbol中添加setPMSymboldateFormatter

 [dateFormatter setAMSymbol:@"am"];
 [dateFormatter setPMSymbol:@"pm"];

完整答案

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *msgDate = [dateFormatter dateFromString:@"2017-05-05T08:27:31.337Z"];
[dateFormatter setDateFormat:@"dd MMM yyyy / HH:mma"];
[dateFormatter setAMSymbol:@"am"];
[dateFormatter setPMSymbol:@"pm"];
NSString *final = [dateFormatter stringFromDate:msgDate];

答案 1 :(得分:0)

请参阅每行的说明或介绍

NSString *str = @"your date here..."; /// here this is your date with format yyyyy-MM-dd'T'HH:mm:ss.SSSZ

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; // here we create NSDateFormatter object for change the Format of date..
[dateFormatter setDateFormat:@"yyyyy-MM-dd'T'HH:mm:ss.SSSZ"]; //// here set format of date which is in your output date (means above str with format)

NSDate *date = [dateFormatter dateFromString: str]; // here you can fetch date from string with define format

dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd MMM yyyy / HH:mma"];// here set format which you want...

NSString *convertedString = [dateFormatter stringFromDate:date]; //here convert date in NSString
NSLog(@"Converted String : %@",convertedString);