近來用到時(shí)間空間,整理一些可能用到的處理NSDate的方法和大家分享,有什么不足的歡迎大家補(bǔ)充。
//獲取當(dāng)前時(shí)間前后幾年幾月幾天的時(shí)間year=1是當(dāng)前時(shí)間的后一年year=-1時(shí)當(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)建一個(gè)日期格式器
[pickerFormatter setDateFormat:@"yyyy-MM"];
NSString *dateString = [pickerFormatter stringFromDate:newdate];
return dateString;
}
比較時(shí)間的大小
[localDate compare:date8]==NSOrderedDescending?
?[localDate compare:date20]==NSOrderedAscending
NSOrderedSame
比較,取得更早或更晚的日期:
NSDate *d = [date1 earlierDate: date2];
NSDate *d = [date1? laterDate: date2]
是否相同日期:
BOOL b = [date1 isEqualToDate: date2];
從某時(shí)間開始經(jīng)過某秒后的日期時(shí)間:
bDate = [aDate initWithTimeInterval:3*60? sinceDate:aDate]; //從aDate過3分鐘
指定某月的末日: (使用前一個(gè)月的第一天來取得)
NSDate *aDate = [inputDateFormatter dateFromString:@"2000/03/01 00:00:00"]; //給定3月1日零點(diǎn)日期
NSDate *bDate = [orgDate initWithTimeInterval:-1*24*60*60? sinceDate:a];// 1日前
NSLog(@"2000年2月的末日 -> %@", bDate);
取得某兩個(gè)時(shí)間相隔多久:
since = [dateA timeIntervalSinceDate: dateB];
只取得日期不要時(shí)間:
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)前所處時(shí)區(qū)
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSLog(@"now = %@", zone);
3.獲取當(dāng)前時(shí)區(qū)和指定時(shí)間差
NSInteger seconds = [zone secondsFromGMTForDate:date];
NSLog(@"seconds = %lu", seconds);
NSDate *nowDate = [date dateByAddingTimeInterval:seconds];
NSLog(@"nowDate = %@", nowDate);
//比較兩個(gè)日期的差值
// 時(shí)間字符串
NSString *str = @"2012-03-11 06:44:11 +0800";
// 1.創(chuàng)建一個(gè)時(shí)間格式化對象
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
// 2.格式化對象的樣式/z大小寫都行/格式必須嚴(yán)格和字符串時(shí)間一樣
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";
// 3.字符串轉(zhuǎn)換成時(shí)間/自動(dòng)轉(zhuǎn)換0時(shí)區(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.獲取了時(shí)間元素
NSDateComponents *cmps = [calendar components:type fromDate:date toDate:now options:0];
NSLog(@"%ld年%ld月%ld日%ld小時(shí)%ld分鐘%ld秒鐘", cmps.year, cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second);