我从Web服务获取日期组件@“2014-01-08T00:00:00.000Z”。我需要将它转换为仅向我提供年,月和日期的日期。请帮忙
答案 0 :(得分:3)
如果您需要输出year-month -date,请使用以下代码。您还需要将格式转换为以下格式: -
NSString *currentDateString = @"2014-01-08T00:00:00.000Z";
self.dateFormatter=[[NSDateFormatter alloc]init];
[self.dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
NSDate *currentDate = [self.dateFormatter dateFromString:currentDateString];
[self.dateFormatter setDateFormat:@"yyyy-MM-dd"];
currentDateString=[self.dateFormatter stringFromDate:currentDate];
NSLog(@"CurrentDate:%@", currentDateString);
<强>输出: - 强>
CurrentDate:2014-01-08
答案 1 :(得分:2)
使用此代码获取当前日期
NSDateFormatter *dateFor=[[NSDateFormatter alloc]init];
NSString *currentDateString = @"2014-01-08T21:21:22.737+05:30";
[dateFor setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *currentDate = [dateFor dateFromString:currentDateString];
NSLog(@"CurrentDate:%@", currentDate);
答案 2 :(得分:1)
再次从字符串中获取日期后,更改dateformatter的格式,然后使用formatter获取字符串。
NSString *currentDateString = @"2014-01-08T21:21:22.737+05:30";
NSDateFormatter *dateFor=[[NSDateFormatter alloc]init];
[dateFor setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"];
NSDate *currentDate = [dateFor dateFromString:currentDateString];
[dateFor setDateFormat:@"MMMM dd, yyyy"];
// above format will show date as Dec 31, 2013 something like this
// can use other format as well like. [dateFor setDateFormat:@"yyyy-MM-dd"]
NSString *dateString = [dateFor stringFromDate:currentDate];