我有一串日期 2010-11-29T00:00:00+05:30,我想将其转换为 NSDate
。
我想将其转换为 11/29/2010。为此,我正在做这样的事情..
NSDateFormatter *df = [[NSDateFormatter alloc] init];
textForLabel = @"2010-11-29T00:00:00+05:30";
[df setDateFormat:@"yyyy-MM-dd"];
NSDate *myDate = [df dateFromString:textForLabel];
NSLog(@"date : %@",myDate);
但输出为空。
2010-11-29T00:00:00+05:30 的日期格式可能是什么?
我不明白这里出了什么问题。
最佳答案
首先,去掉时区中的冒号,因为没有 timezone format identifier对于“+05:30”
textForLabel = [textForLabel
stringByReplacingCharactersInRange:NSMakeRange(21,1)
withString:@""];
然后,您将得到以下字符串:2010-11-29T00:00:00+0530
可以使用以下日期格式:
[df setDateFormat:@"yyyy:MM:dd'T'HH:mm:sszzz"];
关于iphone - 使用 NSDateformatter 将 NSString 转换为 NSDate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11123329/