ios秒轉(zhuǎn)標(biāo)準(zhǔn)時(shí)間(互轉(zhuǎn))

標(biāo)準(zhǔn)時(shí)間即(2017-05-05 17:33:09)字符串;

//時(shí)間戳 轉(zhuǎn) 標(biāo)準(zhǔn)時(shí)間(2017-07-15格式) - wsx注釋
+(NSString *)timeStampSwitchStandardTime:(long long)timeStamp{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeStamp];//秒數(shù)@"1493976789"轉(zhuǎn)時(shí)間    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSString *time = [formatter stringFromDate:date];//時(shí)間轉(zhuǎn)標(biāo)準(zhǔn)時(shí)間字符串
    NSLog(@"標(biāo)準(zhǔn)時(shí)間字符串:%@",time);
    
    return time;
}

打印截圖:


秒轉(zhuǎn)標(biāo)準(zhǔn)時(shí)間.png

進(jìn)階轉(zhuǎn)換,顯示多少秒/分/小時(shí)/天之前,代碼如下:

//時(shí)間戳 轉(zhuǎn) 標(biāo)準(zhǔn)時(shí)間(2017-07-15格式) - wsx注釋
+(NSString *)timeStampSwitchStandardTime:(long long)timeStamp{
    NSDate *nowDate = [NSDate date];
    NSDate *oldDate = [NSDate dateWithTimeIntervalSince1970:timeStamp];//時(shí)間戳轉(zhuǎn)Date時(shí)間
    // 兩個(gè)時(shí)間戳的間隔   返回的是秒
    NSTimeInterval timeInterval = [nowDate timeIntervalSinceDate:oldDate];
    NSInteger interval = (NSInteger)timeInterval;
    //日期獲取 - wsx注釋
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSString *time;
    if (interval < 60 ) {
        time = [NSString stringWithFormat:@"%ld秒前",(long)interval];
    }else if (interval < 60*60){
        time = [NSString stringWithFormat:@"%ld分前",(long)interval/60];
    }else if (interval < 60*60*24){
        time = [NSString stringWithFormat:@"%ld小時(shí)前",(long)interval/3600];
    }else if (interval < 60*60*24*2){
        time = @"昨天";
    }else if (interval < 60*60*24*3){
        time = @"前天";
    }else{
        time = [formatter stringFromDate:oldDate];//時(shí)間轉(zhuǎn)標(biāo)準(zhǔn)時(shí)間字符串
    }
    NSLog(@"標(biāo)準(zhǔn)時(shí)間字符串:%@",time);
    
    return time;
}

標(biāo)準(zhǔn)時(shí)間轉(zhuǎn)時(shí)間戳:

- (NSString *)obtainTimeStampFromStandardTimeWithStartTime:(NSString *)startTime EndTime:(NSString *)endTime {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"]; // hh表示:12小時(shí)制,HH表示:24小時(shí)制
    //設(shè)置時(shí)區(qū),這個(gè)對(duì)于時(shí)間的處理很重要
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [dateFormatter setTimeZone:timeZone];
    NSString *nowTimeStamp = [NSString stringWithFormat:@"%ld", (long)[[NSDate date] timeIntervalSince1970]];
    // 開(kāi)始時(shí)間轉(zhuǎn)換
    NSDate *startDate = [dateFormatter dateFromString:startTime]; // 將standardTime字符串按dateFormatter轉(zhuǎn)成NSDate
    NSString *startTimeStamp = [NSString stringWithFormat:@"%ld", (long)[startDate timeIntervalSince1970]]; // date與當(dāng)前時(shí)間差8小時(shí)  // 得到秒
    // 結(jié)束時(shí)間轉(zhuǎn)換
    NSDate *endDate = [dateFormatter dateFromString:endTime];
    NSString *endTimeStamp = [NSString stringWithFormat:@"%ld", (long)[endDate timeIntervalSince1970]]; // 得到秒
    // 比較時(shí)間差
    if ([[NSDate date] timeIntervalSinceDate:startDate] < 0.0f) {
        return @"未開(kāi)始";
    } else if ([[NSDate date] timeIntervalSinceDate:endDate] > 0.0f) {
        return @"已結(jié)束";
    }
    
    return @"進(jìn)行中";
}
最后編輯于
?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線(xiàn)程,因...
    小菜c閱讀 7,391評(píng)論 0 17
  • 曾經(jīng)有一份美好的愛(ài)情放在我的面前我沒(méi)有珍惜。等到失去后才后悔莫及。如果可以再對(duì)小李說(shuō)。毛欣想說(shuō)。這輩子無(wú)緣再牽手。...
    毛欣與小李閱讀 3,390評(píng)論 0 13
  • 國(guó)家電網(wǎng)公司企業(yè)標(biāo)準(zhǔn)(Q/GDW)- 面向?qū)ο蟮挠秒娦畔?shù)據(jù)交換協(xié)議 - 報(bào)批稿:20170802 前言: 排版 ...
    庭說(shuō)閱讀 12,556評(píng)論 6 13
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,366評(píng)論 25 708
  • 《流淌的春花》 春天把希望來(lái)播灑, 春天有起伏的溫差, 活潑著的盎然生機(jī), 一瞬間的定格勃發(fā), 玉色的奧洲蘭純潔淡...
    獨(dú)行彡閱讀 258評(píng)論 0 0

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