NSDate日常使用方法總結(jié)

(注:持續(xù)更新)
1、時(shí)間轉(zhuǎn)時(shí)間戳:

- (void)intervalTimeWithDate {
    //當(dāng)前時(shí)間
    NSDate *date = [NSDate date];
    
    NSTimeInterval timeInterval = [date timeIntervalSince1970]*1000;
    
    NSLog(@"currentDate timeInterval = %f",timeInterval);
}

2、時(shí)間戳轉(zhuǎn)時(shí)間:

- (void)dateWithIntervalTime {
    //當(dāng)前時(shí)間
    NSTimeInterval timeInterval = 1474283450000;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval/1000];
    
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateStr = [formatter stringFromDate:date];
    NSLog(@"date = %@",dateStr);
}

3、倒計(jì)時(shí)

- (void)timeIntervalCompare {
    
    NSTimeInterval timerInterval1 = 1474283450000;
    NSTimeInterval timerInterval2 = 1473483450000;
    
    NSInteger timeInterval = (timerInterval2 - timerInterval1)/1000;
    
    int day = (int)(timeInterval/86400);
    int hour = (int)((timeInterval - day*86400)/3600);
    int minute = (int)(timeInterval - day*86400 - hour*3600)/60;
    int second = (int)(timeInterval - day*86400 - hour*3600 - minute*60);
    
    NSLog(@"%d天%d時(shí)%d分%d秒",day,hour,minute,second);
    
}
最后編輯于
?著作權(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)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,058評(píng)論 25 709
  • 國(guó)家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說(shuō)閱讀 12,423評(píng)論 6 13
  • 一個(gè) pygame函數(shù)或方法,在傳遞一個(gè)文件路徑的參數(shù)時(shí)將接受unicode或者一個(gè)字節(jié) (8位)或者ASCII碼...
    3767d46199be閱讀 676評(píng)論 0 0
  • 翻出自己畫的一些人物,希望大家給點(diǎn)意見(jiàn)
    COLOR_EDDY閱讀 568評(píng)論 3 5
  • 在一個(gè)無(wú)味的午后 你說(shuō) 來(lái) 抱著你睡 可能是無(wú)聊的說(shuō)辭 隔著屏幕 我卻慌措 因?yàn)?容易當(dāng)真 因?yàn)?體寒最怕被溫暖后的貪戀
    偏執(zhí)狂via閱讀 138評(píng)論 0 0

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