時(shí)間戳跟標(biāo)準(zhǔn)時(shí)間相互轉(zhuǎn)換

本段代碼的主要功能是取出裝滿標(biāo)準(zhǔn)時(shí)間的數(shù)組里邊最大的值。并以標(biāo)準(zhǔn)時(shí)間輸出

    NSMutableArray *arrayMSecond = [NSMutableArray arrayWithCapacity:2];
    for(NSString *date in arrayM){
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateStyle:NSDateFormatterMediumStyle];
        [formatter setTimeStyle:NSDateFormatterShortStyle];
        [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
        //設(shè)置時(shí)區(qū),這個(gè)對(duì)于時(shí)間的處理有時(shí)很重要
        NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
        [formatter setTimeZone:timeZone];
        NSDate* dateSecond = [formatter dateFromString:date]; //------------將字符串按formatter轉(zhuǎn)成nsdate
        //時(shí)間轉(zhuǎn)時(shí)間戳的方法:
        NSInteger timeSp = [[NSNumber numberWithDouble:[dateSecond timeIntervalSince1970]] integerValue];
        [arrayMSecond addObject:@(timeSp)];
    }
    NSInteger max = [[arrayMSecond objectAtIndex:0] integerValue];
    for (int i=0; i<arrayMSecond.count-1; i++) {
        if(max < [[arrayMSecond objectAtIndex:i+1] integerValue]){
            max = [[arrayMSecond objectAtIndex:i+1] integerValue];
        }
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; //----------設(shè)置你想要的格式,hh與HH的區(qū)別:分別表示12小時(shí)制,24小時(shí)制
    //設(shè)置時(shí)區(qū),這個(gè)對(duì)于時(shí)間的處理有時(shí)很重要
    NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:max];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
    self.presellEndDate = confromTimespStr;
最后編輯于
?著作權(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),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 第5章 引用類型(返回首頁) 本章內(nèi)容 使用對(duì)象 創(chuàng)建并操作數(shù)組 理解基本的JavaScript類型 使用基本類型...
    大學(xué)一百閱讀 3,692評(píng)論 0 4
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,711評(píng)論 19 139
  • 如果你問昆明女孩能夠承受的最遠(yuǎn)離家的距離?我的回答是1400公里,從昆明到廣州。 你現(xiàn)在的收入怎么樣? 你現(xiàn)在住的...
    jasmine_LL閱讀 553評(píng)論 1 0
  • 一、基本內(nèi)置類型 C++定義的幾種基本的算術(shù)類型:int,char,float和bool。以及特殊的void類型,...
    LuuilX閱讀 2,675評(píng)論 5 1
  • 六月的激情 已經(jīng)點(diǎn)燃 高考的日子 如期而至 成長的歲月,從此開始 孩子們,加油! 大戰(zhàn)之前須靜氣 不要慌,不要亂 ...
    松風(fēng)入骨閱讀 386評(píng)論 35 31

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