我有这样的时间格式: - 2014-09-10T16:04:44
我使用下面的代码将其转换为NSString
,但我得到的值为NULL
NSDateFormatter *dateformatter=[[NSDateFormatter alloc]init];
[dateformatter setDateFormat:@"MM-dd-yyyy HH:mm:ss"];
NSDate *dateFromString = [[NSDate alloc] init];
dateFromString = [dateformatter dateFromString:obj.strResultDate];
有谁能告诉我我做错了什么?我认为它必须是输入时间格式,但我不知道是什么。
答案 0 :(得分:0)
这就是我用的:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:@"HH:mm:ss"];
NSDate *now = [[NSDate alloc] init];
NSString *theDate = [dateFormat stringFromDate:now];
NSString *theTime = [timeFormat stringFromDate:now];
NSLog(@"\n"
"theDate: |%@| \n"
"theTime: |%@| \n"
, theDate, theTime);
答案 1 :(得分:0)
要将此字符串转换为日期,请执行以下操作:
NSString *dateString = @"2014-09-10T16:04:44";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
NSDate *convertedDate = [dateFormatter dateFromString:dateString];
NSLog(@"date: %@", convertedDate);
这可能会有所帮助!