Date時(shí)間基礎(chǔ)記錄

一.時(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í),方便以后查閱。

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

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

  • 在iOS開發(fā)中,經(jīng)常會(huì)遇到各種各樣的時(shí)間問題,8小時(shí)時(shí)差,時(shí)間戳,求時(shí)間間隔,農(nóng)歷等等。解決辦法網(wǎng)上比比皆是,但大...
    真巧了_嘿閱讀 2,930評(píng)論 0 7
  • 作品鏈接:http://www.itdecent.cn/users/1e0f5e6f73f6/top_articl...
    打電話記錯(cuò)號(hào)碼的人閱讀 29,434評(píng)論 0 26
  • ######先說下需求:選擇日期彈出日歷(跟途牛,攜程等差不多就行。。。行) 初識(shí)NSCalendar到寫完日歷的...
    只是個(gè)少年閱讀 1,169評(píng)論 0 0
  • iOS開發(fā)中,經(jīng)常會(huì)遇到各種各樣的時(shí)間問題,8小時(shí)時(shí)差,時(shí)間戳,求時(shí)間間隔,農(nóng)歷等等。解決辦法網(wǎng)上比比皆是,但大多...
    小李龍彪閱讀 6,747評(píng)論 1 6
  • 你發(fā)現(xiàn)了嗎 所有的作品 并不是 一開始就很好的 比如歌要聽很多遍 比如詩要讀很多遍 比如畫要不斷不斷 盯著看很久很...
    天野丟閱讀 223評(píng)論 4 3

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