iOS NSDate 時間日期

近來用到時間空間,整理一些可能用到的處理NSDate的方法和大家分享,有什么不足的歡迎大家補(bǔ)充。

//獲取當(dāng)前時間前后幾年幾月幾天的時間year=1是當(dāng)前時間的后一年year=-1時當(dāng)前的前一年,月和日也是一樣

-(void)getTime

{

NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

NSDateComponents *comps = nil;

comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];

NSDateComponents *adcomps = [[NSDateComponents alloc] init];

[adcomps setYear:year];

[adcomps setMonth:month];

[adcomps setDay:day];

NSDate *newdate = [calendar dateByAddingComponents:adcomps toDate:[NSDate date] options:0];

NSDateFormatter *pickerFormatter = [[NSDateFormatter alloc] init];// 創(chuàng)建一個日期格式器

[pickerFormatter setDateFormat:@"yyyy-MM"];

NSString *dateString = [pickerFormatter stringFromDate:newdate];

return dateString;

}

比較時間的大小

[localDate compare:date8]==NSOrderedDescending?

?[localDate compare:date20]==NSOrderedAscending

NSOrderedSame

比較,取得更早或更晚的日期:

NSDate *d = [date1 earlierDate: date2];

NSDate *d = [date1? laterDate: date2]

是否相同日期:

BOOL b = [date1 isEqualToDate: date2];

從某時間開始經(jīng)過某秒后的日期時間:

bDate = [aDate initWithTimeInterval:3*60? sinceDate:aDate]; //從aDate過3分鐘

指定某月的末日: (使用前一個月的第一天來取得)

NSDate *aDate = [inputDateFormatter dateFromString:@"2000/03/01 00:00:00"]; //給定3月1日零點日期

NSDate *bDate = [orgDate initWithTimeInterval:-1*24*60*60? sinceDate:a];// 1日前

NSLog(@"2000年2月的末日 -> %@", bDate);

取得某兩個時間相隔多久:

since = [dateA timeIntervalSinceDate: dateB];

只取得日期不要時間:

NSDateFormatter *df = [[NSDateFormatter alloc] init];

[df setTimeStyle: NSDateFormatterNoStyle];

[df setDateStyle: NSDateFormatterMediumStyle];

NSString *nowDateStr = [df stringFromDate:[NSDate date]];

NSDate *nowDate = [df dateFromString:nowDateStr];

NSLog(@"%@", nowDate);


2.獲取當(dāng)前所處時區(qū)

NSTimeZone *zone = [NSTimeZone systemTimeZone];

NSLog(@"now = %@", zone);

3.獲取當(dāng)前時區(qū)和指定時間差

NSInteger seconds = [zone secondsFromGMTForDate:date];

NSLog(@"seconds = %lu", seconds);

NSDate *nowDate = [date dateByAddingTimeInterval:seconds];

NSLog(@"nowDate = %@", nowDate);

//比較兩個日期的差值

// 時間字符串

NSString *str = @"2012-03-11 06:44:11 +0800";

// 1.創(chuàng)建一個時間格式化對象

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

// 2.格式化對象的樣式/z大小寫都行/格式必須嚴(yán)格和字符串時間一樣

formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";

// 3.字符串轉(zhuǎn)換成時間/自動轉(zhuǎn)換0時區(qū)/東加西減

NSDate *date = [formatter dateFromString:str];

NSDate *now = [NSDate date];

// 注意獲取calendar,應(yīng)該根據(jù)系統(tǒng)版本判斷

NSCalendar *calendar = [NSCalendar currentCalendar];

NSCalendarUnit type = NSCalendarUnitYear |

NSCalendarUnitMonth |

NSCalendarUnitDay |

NSCalendarUnitHour |

NSCalendarUnitMinute |

NSCalendarUnitSecond;

// 4.獲取了時間元素

NSDateComponents *cmps = [calendar components:type fromDate:date toDate:now options:0];

NSLog(@"%ld年%ld月%ld日%ld小時%ld分鐘%ld秒鐘", cmps.year, cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second);

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容