我从@“2014-01-08T00:00:00.000Z”从Web服务获取日期组件。我需要将它转换为只提供年份,月份和日期的日期。请帮助将特定格式转换为NSDate
-4
A
回答
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);
3
如果需要输出年 - 月-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
+0
@Hussan Shabbir我编辑答案。你的回答完美无需编辑抱歉我的错误。 –
+0
其正常np :) –
+0
谢谢你采取冷静.. –
1
正从字符串的日期再次改变dateformatter的格式,然后经过使用获得的字符串格式化。
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];
相关问题
- 1. Swift将特定格式的字符串转换为NSDate
- 2. 将时间戳转换为nsdate格式
- 3. 将NSString转换为NSDate格式?
- 4. 用新格式将NSString转换为NSDate
- 5. 将格式化日期转换为NSDate
- 6. 的NSString的NSDate转换为特定的格式
- 7. 将日期转换为特定格式
- 8. NSDate转换为24小时格式
- 9. 特定格式的NSDate
- 10. 如果iPhone/iPad采用24小时格式,将NSDate格式转换为NSDate时,NSDate即将到来
- 11. 如何将NSDate以特定的日期格式发送为NSNumber?即用另一种格式将NSDate转换为NSDate?
- 12. 如何的NSString转换成NSDate的特定格式
- 13. 将NSDate的描述转换为NSDate?
- 14. 将NSString转换为NSDate
- 15. SWIFT将NSTimeInteval转换为NSDate
- 16. 将NSDateComponents转换为NSDate
- 17. 将NSString转换为NSDate
- 18. 将NSString转换为NSDate
- 19. 将NSDate转换为SWIFT中特定时区的字符串
- 20. 将NSString日期转换为NSDate或特定的字符串
- 21. 如何将表格行转换为特定组格式
- 22. 将NSDate转换为长格式字符串并返回?
- 23. 如何将字符串转换为NSDate格式?
- 24. 如何将nsdate转换为另一种格式使用nsdateformatter
- 25. 将NSDate转换为Sql Server日期时间格式
- 26. 应该将字符串转换为NSDate的格式是什么?
- 27. iPhone:如何将“yyyyMMddThhmmss”格式的字符串转换为NSDate?
- 28. 将MM-dd-yy格式的NSString转换为NSDate
- 29. 将12小时时间格式的字符串转换为NSDate
- 30. 将NSString转换为NSDate并返回到不同的格式NSString
http://unicode.org/reports/tr35/tr35-6.html#Date%5FFormat%5FPatterns – Desdenova
这个问题已经被问超过10000次,下一次尝试使用谷歌 –