一.時(shí)間戳
二.Date的基礎(chǔ)介紹
三.NSCalendar的基礎(chǔ)介紹
一.時(shí)間戳
- 時(shí)間戳:從1970年1月1號(hào) 00:00:00開始走過的毫秒數(shù)。IOS端默認(rèn)生成的時(shí)間戳是10位的,如果服務(wù)器返回的13位需要除以1000例如:
//字符串 -> 時(shí)間戳
NSString * timeStampString = @"1423189125435"
NSDate *date = [NSDate dateWithTimeIntervalSince1970:[timeStampString doubleValue] / 1000];
- 時(shí)間戳轉(zhuǎn)Date
NSDate *date = [NSDate dateWithTimeIntervalSince1970:second];
二.Date的基礎(chǔ)介紹
1.時(shí)間和時(shí)區(qū) (北京為東8區(qū))
- 創(chuàng)建date對(duì)象即時(shí)間對(duì)象
NSDate *dats = [[NSDate alloc]init]; - 獲取當(dāng)前時(shí)間(本初子午線的時(shí)間)
NSDate *date = [NSDate date]; - 獲取當(dāng)前所在地區(qū)的時(shí)區(qū)
NSTimeZone *zone = [NSTimeZone systemTimeZone]; - 獲取當(dāng)前時(shí)區(qū)和指定時(shí)間的時(shí)差
NSTimeInterval seconds = [zone secondsFromGMTForDate:date];
NSDate *nowDate = [date dateByAddingTimeInterval:secend];
2.字符串于Date的相互轉(zhuǎn)化
- 字符串-> Date
NSString *createdString = @"2018-11-20";
//時(shí)間格式化對(duì)象
NSDateFormatter *dateformate = [[NSDateFormatter alloc]init];
dateformate.dateFormat = @"yyyy-MM-dd";
//會(huì)自動(dòng)減去8個(gè)小時(shí)
NSDate *creatDate = [dateformate dateFromString:createdString];
NSLog(@"%@",creatDate);
- Date ->字符串
NSDate *nowdate = [NSDate date]; NSString *nowstring = [dateformate stringFromDate:nowdate];
總結(jié):Date 和字符串相互轉(zhuǎn)換需要一個(gè)橋梁NSDateFormatter,NSDateFormatter 的格式有很多比如:yyyy/MM/dd HH/mm/ss 、yyyy年MM月dd日 HH時(shí)mm分ss秒、yyyy/MM/dd等。
3.通過Date比較兩時(shí)間 (NSComparisonResult)
NSString *createdString = @"2018-11-20";
NSDateFormatter *dateformate = [[NSDateFormatter alloc]init];
dateformate.dateFormat = @"yyyy-MM-dd";//自動(dòng)減去8個(gè)小時(shí)
NSDate *creatDate = [dateformate dateFromString:createdString];
NSLog(@"%@",creatDate);
NSDate *nowdate = [NSDate date];
NSComparisonResult result = [creatDate compare:nowdate];
if (result == NSOrderedAscending) {
NSLog(@"nowdate>creatDate");
}
else if(result == NSOrderedDescending)
{
NSLog(@"creatDate>nowdate");
}
else if(result == NSOrderedSame)
{
NSLog(@"creatDate=nowdate");
}
4.時(shí)間的加減 (通過時(shí)間戳)
NSDate*nowDate = [NSDate date];
NSTimeInterval interval =24*60*60*1; //1:天數(shù)
NSDate*date1 = [nowDate initWithTimeIntervalSinceNow:+interval];//加一天
NSDate*date1 = [nowDate initWithTimeIntervalSinceNow:-interval];//減一天
三.NSCalendar的基礎(chǔ)介紹
NSDate * date = [NSDate date];
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 指定日歷的算法 NSCalendarIdentifierGregorian,NSGregorianCalendar
// NSDateComponent 可以獲得日期的詳細(xì)信息,即日期的組成
NSDateComponents *comps = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond|NSCalendarUnitWeekOfMonth|NSCalendarUnitWeekday fromDate:date];
NSLog(@"年 = year = %ld",comps.year);
NSLog(@"月 = month = %ld",comps.month);
NSLog(@"日 = day = %ld",comps.day);
NSLog(@"時(shí) = hour = %ld",comps.hour);
NSLog(@"分 = minute = %ld",comps.minute);
NSLog(@"秒 = second = %ld",comps.second);
NSLog(@"星期 =weekDay = %ld ",comps.weekday);// 從周日開始算的
-
通過NSCalendar日期比較
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; // 指定日歷的算法
NSDate * currentDate = [NSDate date]; // 這個(gè)日期可以你自己給定
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay
inUnit: NSCalendarUnitMonth
forDate:currentDate];
NSLog(@"%ld",range.length);//日期比較 NSDateComponents *cmps = [calendar components:NSCalendarUnitYear fromDate:[NSDate dateWithTimeIntervalSince1970:0] toDate:[NSDate date] options:0]; NSLog(@"%li",(long)cmps.year);
NSCalendarUnitYear這個(gè)參數(shù)可選:
NSCalendarUnitYear
NSCalendarUnitMonth
NSCalendarUnitDay
NSCalendarUnitHour
NSCalendarUnitMinute
NSCalendarUnitSecond
NSCalendarUnitWeekday
最后寫一個(gè)小的方法,項(xiàng)目中經(jīng)常用到字符串類型的時(shí)間大小對(duì)比
//對(duì)比兩個(gè)時(shí)間大小。
-(BOOL)comparedate:(NSString *)firstTime secendtime:(NSString *)secendTime andNSDateFormatter:(NSDateFormatter *)formatter
{
NSDateFormatter *nowformatter;
if (formatter) {
nowformatter = formatter;
}
else
{
nowformatter = [[NSDateFormatter alloc]init];
nowformatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
}
NSDate *firestDate =[nowformatter dateFromString:firstTime];
NSDate *secendDate = [nowformatter dateFromString:secendTime];
NSComparisonResult result = [firestDate compare:secendDate];
if (result == NSOrderedAscending) {
return NO;
}
else if(result == NSOrderedDescending)
{
return YES;
}
else
{
return YES;
}
}
以上都是一些基礎(chǔ)知識(shí),方便以后查閱。