我得到的日期字符串是这种格式的 2014-01-08T21:21:22.737+05:30。我如何将其授予 NSDate?
我试过了:
NSString *currentDateString = @"2014-01-08T21:21:22.737+05:30";
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS+05:30"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(@"CurrentDate:%@", currentDate);
它返回零。有什么想法吗?
使用此代码它工作正常,您的dateFormatter
错了。
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
NSString *currentDateString = @"2014-01-08T21:21:22.737+05:30";
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(@"CurrentDate:%@", currentDate);
快乐编码!!!!!!!!!.
日期格式包含时区作为文字:yyyy-MM-dd'T'HH:mm:ss.SSS+05:30
时区定义+05:30
。您应该使用 Z
解析它。
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
NSString *currentDateString = @"2014-01-08T21:21:22.737+05:30";
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *currentDate = [dateFormatter dateFromString:currentDateString];
NSLog(@"CurrentDate:%@", currentDate);
NSString *p=@"2014-01-08T21:21:22.737+05:30";
NSDateFormatter *dateformat=[[NSDateFormatter alloc]init];
[dateformat setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *datefor=[dateformat dateFromString:p];
[dateformat setDateFormat:@"MM-dd-yyyy hh:mm a"];
NSString *dateStr=[dateformat stringFromDate:datefor];
NSDate *datetype=[dateformat dateFromString:dateStr];
我认为您需要在代码中像下面这样初始化dateFormatter
。因为如果不总是初始化,您将获得空值:-
dateFormatter=[[NSDateFormatter alloc]init];
将日期格式设置为
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
是的,现在 NSLog 正在打印一个特定的日期!!
享受编码