2011-05-14 108 views
2

如何將2011-05-08T22:08:38Z轉換爲目標C中的相對時間格式?將時間格式轉換爲相對時間目標C

+0

你的意思是'當地時間'嗎?如果不是,那麼'相對時間'是什麼意思? – 2011-05-14 20:36:44

+0

你可能會考慮看看[我的NSDateFormatter只適用於iPhone模擬器](http://stackoverflow.com/questions/838893/my-nsdateformatter-works-only-in-the-iphone-simulator)。它不是直接重複的,但非常接近。 – 2011-05-14 20:39:22

+0

[相對字符串從NSDate]可能的重複(http://stackoverflow.com/questions/4937230/relative-string-from-nsdate) – 2011-05-14 23:48:30

回答

4
// create a date formatter 
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
// set the formatter to parse RFC 3339 dates 
[formatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"]; 
// the formatter parses your date into an NSDate instance 
NSDate *date = [formatter dateFromString:@"2011-05-08T22:08:38Z"]; 

// now to get relative information you can do things like this, which 
// will give you the number of seconds since now 
NSTimeInterval secondsFromNow = [date timeIntervalSinceNow]; 

// or this, which will give you the time interval between this data 
// and another date 
NSTimeInterval secondsFromOther = [date timeIntervalSinceDate:someOtherDateInstance]; 
+1

甜。非常感謝。我只是努力得到正確的格式。 – Menan 2011-05-16 22:26:13

+0

+1很好的答案。謝謝 – 2013-02-12 13:52:00

+0

謝謝你的可重用的答案! – trillions 2013-02-21 19:30:44