1

我收到来自 Web 服务日期的响应,例如“2012-08-17T00:00:00”。我只想显示日期,例如 17-08-2012。如何更改日期格式并删除该时间...

4

2 回答 2

2
NSDate *today = [NSDate date];
NSLog(@"Date today: %@", today);

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init] ;
dateFormat.dateFormat =  @"dd-MM-yyyy";
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(@"Date: %@", dateString);

另请参阅以下日期和时间格式样式

NSDateFormatterNoStyle
NSDateFormatterShortStyle
NSDateFormatterMediumStyle
NSDateFormatterLongStyle
NSDateFormatterFullStyle

你可以像这样设置 [dateFormat setDateStyle:NSDateFormatterShortStyle];

于 2012-08-16T09:37:55.263 回答
2
//String froms web service(ws) 

NSString *stringFromWS =[NSString stringWithString:@"2012-08-17T00:00:00"];


//date formatter for the above string
NSDateFormatter *dateFormatterWS = [[NSDateFormatter alloc] init];
[dateFormatterWS setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];

NSDate *date =[dateFormatterWS dateFromString:stringFromWS];


//date formatter that you want 

NSDateFormatter *dateFormatterNew = [[NSDateFormatter alloc] init];
[dateFormatterNew setDateFormat:@"dd-MM-yyyy"];

NSString *stringForNewDate = [dateFormatterNew stringFromDate:date];

NSLog(@"Date %@",stringForNewDate);

另请参阅链接以获取更多说明 http://mobiledevelopertips.com/cocoa/date-formatters-examples-take-2.html

于 2012-08-16T10:19:23.347 回答