如何在iphone sdk中更改日期格式 [英] How to change the date format in iphone sdk

查看:87
本文介绍了如何在iphone sdk中更改日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Webservices日期获得响应,如2012-08-17T00:00:00。我想显示日期只有17-08-2012。如何更改日期格式并删除该时间......

I'm getting Response from Webservices Date like "2012-08-17T00:00:00". I want to Display Date only like 17-08-2012. How Change Date format and Remove that time...

推荐答案

//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

also refer the link for more explanation http://mobiledevelopertips.com/cocoa/date-formatters-examples-take-2.html

这篇关于如何在iphone sdk中更改日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆