> > 取當前時間的秒數 > NSTimeInterval time = [[NSDate date] timeIntervalSince1970]; > long long int date = (long long int)time; > NSLog(@」date\n%d」, date); //1295322949 > //把秒數轉化成yyyy-MM-dd hh:mm:ss格式 > NSDate *dd = [NSDate dateWithTimeIntervalSince1970:date]; > NSLog(@」d:%@」,dd); //2011-01-18 03:55:49 +0000 > //給一個時間秒數,取出對應的時間 > NSString *s = @」1295355600000″; //對應21:00 > NSDate *d = [NSDate dateWithTimeIntervalSince1970:[s doubleValue]/1000]; > NSLog(@」dddd:%@」,d); //2011-01-18 13:00:00 +0000 > NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init]; > [formatter1 setDateFormat:@"HH:mm"]; > NSString *showtimeNew = [formatter1 stringFromDate:d]; > NSLog(@」showtimeNew:%@」,showtimeNew); //21:00 比d的時間 +8小時 > [formatter1 release]; > > 1. 建立或初始化可用如下方法 > 用於建立NSDate實例的類方法有 > + (id)date; > 返回當前時間 > + (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs; > 返回以當前時間爲基準,而後過了secs秒的時間 > + (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs; > 返回以2001/01/01 GMT爲基準,而後過了secs秒的時間 > + (id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs; > 返回以1970/01/01 GMT爲基準,而後過了secs秒的時間 > + (id)distantFuture; > 返回不少年之後的將來的某一天。(好比你須要一個比如今(Now)晚(大)很長時間的時間值,則能夠調用該方法。測試返回了4000/12/31 16:00:00) > + (id)distantPast; > 返回不少年之前的某一天。(好比你須要一個比如今(Now)早(小)大很長時間的時間值,則能夠調用該方法。測試返回了公元前0001/12/31 17:00:00) > 用於建立NSDate實例的實例方法有 > - (id)addTimeInterval:(NSTimeInterval)secs; > 返回以目前的實例中保存的時間爲基準,而後過了secs秒的時間 > 用於初始化NSDate實例的實例方法有 > - (id)init; > 初始化爲當前時間。相似date方法 > 初始化爲以2001/01/01 GMT爲基準,而後過了secs秒的時間。相似dateWithTimeIntervalSinceReferenceDate:方法 > - (id)initWithTimeInterval:(NSTimeInterval)secs sinceDate:(NSDate *)refDate; > 初始化爲以refDate爲基準,而後過了secs秒的時間 > - (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs; > 初始化爲以當前時間爲基準,而後過了secs秒的時間 > 2. 日期之間比較可用如下方法 > - (BOOL)isEqualToDate:(NSDate *)otherDate; > 與otherDate比較,相同返回YES > - (NSDate *)earlierDate:(NSDate *)anotherDate; > 與anotherDate比較,返回較早的那個日期 > - (NSDate *)laterDate:(NSDate *)anotherDate; > 與anotherDate比較,返回較晚的那個日期 > - (NSComparisonResult)compare:(NSDate *)other; > 該方法用於排序時調用: > . 當實例保存的日期值與anotherDate相同時返回NSOrderedSame > . 當實例保存的日期值晚於anotherDate時返回NSOrderedDescending > . 當實例保存的日期值早於anotherDate時返回NSOrderedAscending > 3. 取回時間間隔可用如下方法 > - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate; > 以refDate爲基準時間,返回實例保存的時間與refDate的時間間隔 > - (NSTimeInterval)timeIntervalSinceNow; > 以當前時間(Now)爲基準時間,返回實例保存的時間與當前時間(Now)的時間間隔 > - (NSTimeInterval)timeIntervalSince1970; > 以1970/01/01 GMT爲基準時間,返回實例保存的時間與1970/01/01 GMT的時間間隔 > - (NSTimeInterval)timeIntervalSinceReferenceDate; > 以2001/01/01 GMT爲基準時間,返回實例保存的時間與2001/01/01 GMT的時間間隔 > + (NSTimeInterval)timeIntervalSinceReferenceDate; > 以2001/01/01 GMT爲基準時間,返回當前時間(Now)與2001/01/01 GMT的時間間隔 > 4. 將時間表示成字符串 > - (NSString *)description; > 以YYYY-MM-DD HH:MM:SS ±HHMM的格式表示時間。(其中 「±HHMM」 表示與GMT的存在多少小時多少分鐘的時區差別。好比,若時區設置在北京,則 「±HHMM」 顯示爲 「+0800″) > > 將時區設成本地: > NSTimeZone *zone = [NSTimeZonesystemTimeZone]; > > NSInteger interval = [zone secondsFromGMTForDate:date]; > > NSDate *localeDate = [date addTimeInterval:interval]; > > > NSString和NSDate互轉須要用到NSDateFormatter,設置一下timezone和format便可,直接上代碼 > > > > > > NSString和NSDate互轉須要用到NSDateFormatter,設置一下timezone和format便可,直接上代碼 > [cpp] view plaincopy > NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; > NSTimeZone *timeZone = [NSTimeZone localTimeZone]; > > [formatter setTimeZone:timeZone]; > [formatter setDateFormat : @"M/d/yyyy h:m a"]; > > NSString *stringTime = @"12/5/2011 3:4 am"; > > NSDate *dateTime = [formatter dateFromString:stringTime]; > > NSLog(@"%@", dateTime);//打印2011-12-04 19:04:00 +0000,這裏+0000表示時區 > > NSDate *dateNow = [NSDate date]; > > NSLog(@"%@", dateNow);//打印2011-08-17 08:26:57 +0000,這裏+0000表示時區 > > [formatter setDateFormat : @"yyyy年M月d日 H點m分"]; > > NSLog(@"%@", [formatter stringFromDate:dateNow]);//打印2011年8月17日 16點26分 > > 關於format中的每一個字母表明的意思,能夠到這裏去查:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/DataFormatting/Articles/df100103.html#//apple_ref/doc/uid/TP40007972-SW1,拉到中間的部分便可看到 > > > 今天發現用NSDateFormatter的stringFromDate方法將服務器傳過來的背景時間的NSDate轉換成NSString會相差8小時,調查發現,多是由於stringFromDate時,將date當作格林威治時間了,返回的String變成本地時區。因此差了8個小時。 > > 我用的方式是: > > 1 > NSDateFormatter* formate=[[NSDateFormatter alloc]init]; > 2 > [formate setDateFormat:DATE_FORMAT_SPLIT]; > 3 > [formate setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; > 4 > [formate stringFromDate:date]這樣再轉換時就不會出現時區問題了。 > 固然,好像方法不是惟一的。 > > > > 1.4.2 日期和時間 > > 咱們使用NSDate類比較日期,並計算兩個日期之間的日期和時間間隔: > > 能夠用當前的日期和時間建立一個NSDate: > > NSDate *myDate = [NSDate date]; > 能夠建立一個NSDate,表示從如今開始的24小時: > > NSTimeInterval secondsPerDay = > > 4*60*60; > NSDate *tomorrow = [NSDate > -ateWithTimeIntervalSinceNow: > -econdsPerDay]; > 可使用以下代碼,根據一個已有的日期建立一個日期: > > NSTimeInterval secondsPerDay = > > 4*60*60; > NSDate *now = [NSDate date]; > NSDate *yesterday = [now > -ddTimeInterval:-secondsPerDay]; > 能夠比較兩個日期是否徹底相等: > > BOOL sameDate = > -date1 isEqualToDate:date2]; > 或者,判斷一個日期是在另外一個日期以前仍是以後,使用以下代碼: > > NSDate *earlierDate = > -date1 earlierDate:date2]; > NSDate *laterDate = > -date1 laterDate:date2]; > 能夠計算兩個日期之間相隔多少秒: > > NSTimeInterval secondsBetweenDates = > > > -date2 timeIntervalSinceDate: > -ate1]; > 或者能夠計算如今和未來的一個日期之間相隔多少秒: > > NSTimeInterval secondsUntilTomorrow > [tomorrow timeIntervalSinceNow]; > 經過使用NSCalendar類,咱們能夠更加容易地建立NSDate對象。 > > 例如,建立一個表示2010年6月1日的日期,使用以下代碼: > > NSDateComponents *comp = > -[NSDateComponents alloc] init]; > [comp setMonth:06]; > [comp setDay:01]; > [comp setYear:2010]; > NSCalendar *myCal = [[NSCalendar > -lloc] initWithCalendarIdentifier: > -SGregorianCalendar]; > NSDate *myDate = > -myCal dateFromComponents:comp]; > 相似地,從一個已有的日期中獲取日期、月份和年份等組成部分,使用以下代碼: > > unsigned units = NSMonthCalendarUnit > - NSDayCalendarUnit | > NSYearCalendarUnit; > NSDate *now =[NSDate date]; > NSCalendar *myCal = [[NSCalendar > -lloc] initWithCalendarIdentifier: > -SGregorianCalendar]; > NSDateComponents *comp = [myCal > -omponents:units fromDate:now]; > NSInteger month = [comp month]; > NSInteger day = [comp day]; > NSInteger year = [comp year]; > 當經過已有的日期建立日期的時候,建立日曆也會變得更容易一些,由於咱們沒必要把全部內容轉換爲秒以及從秒轉換過來。 > > 例如,從新編寫前面建立的一個表示明天日期的例子,使用以下的代碼: > > NSDate *now = [NSDate date]; > NSDateComponents *comp = > -[NSDateComponents alloc] init]; > [comp setDay:01]; > NSCalendar *myCal = [[NSCalendar > -lloc] initWithCalendarIdentifier: > -SGregorianCalendar]; > NSDate *tomorrow = [myCal > -ateByAddingComponents:comp > -oDate:now options:0]; > 當咱們想要把可供人類閱讀的日期和時間顯示給用戶,NSDate自己並非特別容易實現。所以,咱們一般會使用NSDateFormatter。 > > 使用NSDateFormatter得到表示當前日期的一個字符串,使用以下代碼: > > NSDate *now = [NSDate date]; > NSDateFormatter *formatter = > -[NSDateFormatter alloc] init]; > [formatter setDateStyle: > -SDateFormatterMediumStyle]; > NSString *friendlyDate = > -formatter stringFromDate:now]; > 獲取當前時間,可使用以下代碼: > > NSDate *now = [NSDate date]; > NSDateFormatter *formatter = > -[NSDateFormatter alloc] init]; > [formatter setTimeStyle: > -SDateFormatterMediumStyle]; > NSString *friendlyTime = > -formatter stringFromDate:now]; > 表1-2給出了5種預約義的格式化樣式。 > > 最後,也可使用一個日期格式的dateFormat屬性來手動地設置樣式: > > NSDate *now = [NSDate date]; > NSDateFormatter *formatter = > -[NSDateFormatter alloc] init]; > [formatter setDateFormat: > -"yyyy-mm-dd"]; > NSString *friendlyDate = > -formatter stringFromDate:now]; > 注意formatter的格式大小寫必須嚴格按照文檔提供的,不然會出現錯誤! >