2012-06-18 131 views
-1

即時通訊試圖找出如何轉換時間戳日期和時間(格式 - 2012-06-14T13:15:21Z)以顯示在屏幕上像'1小時前'/'2天前'/ '2秒前'等任何人都可以幫助我呢?轉換時間戳 - IPhone

+0

你已經嘗試什麼,以及您所面臨的問題? – rishi

+0

[iPhone:將日期字符串轉換爲相對時間戳]的可能重複(http://stackoverflow.com/questions/902950/iphone-convert-date-string-to-a-relative-time-stamp) –

回答

2

試試這個:

NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
[cal setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; 
NSDate *date1 = yourDate; 
NSDate *date2 = [NSDate date];//Current Date 
unsigned int unitFlags = NSDayCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;     
NSDateComponents *diffComps = [cal components:unitFlags fromDate:date2 toDate:date1 options:0]; 
[diffComps month] //gives you month 
[diffComps day] //gives you day 
[diffComps year] // gives you year 
[cal release]; 
+0

謝謝很多朋友... :) :) –