我有从Web服务返回的当前时间格式:
2012-06-25T23:45:52.664Z
我希望得到类似的内容:
2012 june 25 23:45
所以没有秒和Z格式指示器。
我的相关代码是:
myConvertedTime = [[myActualTime dateFromString] stringFromDate];
答案 0 :(得分:2)
试试这些
NSDate * dateTemp = [[NSDate alloc] init]; NSDateFormatter * dateFormat1 = [[NSDateFormatter alloc] init];
[dateFormat1 setDateFormat:@“yyyy MMM dd hh:mm”];
dateTemp = [dateFormat1 dateFromString:newInvoice.date];
答案 1 :(得分:1)
试试这个:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy MMMM dd HH:mm"];
NSString *myConvertedTime = [formatter stringFromDate:now];
答案 2 :(得分:1)
您应该使用NSDateFormatter
对象。而不是自己指定格式,最好让框架根据用户的国际化设置选择适合的格式。这样做如下:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterLongStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
myConvertedTime = [formatter stringForObjectValue:[myActualTime dateFromString]];
[formatter release];
答案 3 :(得分:1)
请尝试以下代码:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss"];
NSString *dateString = @"2012-06-25T23:45:52.664Z";
dateString = [dateString substringToIndex:[dateString length]-5];
NSDate *date = [dateFormatter dateFromString:dateString];
[dateFormatter release];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:@"yyyy MMMM dd HH:mm"];
NSString *dateText = [dateFormatter2 stringFromDate:date];
[dateFormatter2 release];
NSLog(@"the date is:%@",dateText);