您正在从服务器获取日期,例如“2013-11-18T13:21:51”格式,并尝试设置日期格式以从字符串中获取日期,但它始终显示为空。
-(NSString*)convertStringToDate:(NSString*)fromdate
{
// CreatedDateTime = "2013-11-18T13:21:51";
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[formatter setLocale:[NSLocale systemLocale]];
[formatter setDateFormat:@"YYYY-MM-ddTHH:mm:ss"];
NSDate * from = [formatter dateFromString:fromdate];
NSLog(@"date %@ ",from);
NSDateFormatter* df = [[NSDateFormatter alloc]init];
[formatter setLocale:[NSLocale systemLocale]];
[df setDateFormat:@"HH:mm"];
NSLog(@"date string %@ ",[df stringFromDate:from]);
return [NSString stringWithFormat:@"%@",[df stringFromDate:from]];
}
请纠正我在哪里做错了。
答案 0 :(得分:2)
替换
[formatter setDateFormat:@"YYYY-MM-ddTHH:mm:ss"]
;
使用
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];