我從@「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次,下一次嘗試使用谷歌 –