iOS開發(fā)時(shí)間與時(shí)間戳轉(zhuǎn)換(毫秒級(jí))

??時(shí)間戳轉(zhuǎn)化為時(shí)間??

//將時(shí)間戳轉(zhuǎn)換為時(shí)間 (以毫秒為單位)
//參數(shù):時(shí)間戳
//返回值格式:2019-04-19 10:33:35.886
- (NSString *)getTimestamp:(NSString*)mStr{
    
    NSTimeInterval interval    =[mStr doubleValue] / 1000.0;
    
    NSDate *date              = [NSDate dateWithTimeIntervalSince1970:interval];
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
    
    NSString *dateString      = [formatter stringFromDate: date];
    
    NSLog(@"時(shí)間戳對(duì)應(yīng)的時(shí)間是:%@",dateString);
    
    return dateString;
    
}

//獲取當(dāng)前時(shí)間 (以毫秒為單位)
//返回值格式:2019-04-19 10:33:35.886
- (NSString *)getNowTimeTimestamp{
    
    NSDate *datenow = [NSDate date];//現(xiàn)在時(shí)間,你可以輸出來看下是什么格式
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    
    [formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
    
    NSString *dateString      = [formatter stringFromDate: datenow];
    
    NSLog(@"當(dāng)前時(shí)間戳對(duì)應(yīng)的時(shí)間是:%@",dateString);
    
    return dateString;
}

??時(shí)間轉(zhuǎn)化為時(shí)間戳??

//將時(shí)間轉(zhuǎn)化成 時(shí)間戳
//返回值格式:1555642454396
- (NSInteger)timeSwitchTimestamp:(NSString *)formatTime{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"]; //(@"YYYY-MM-dd hh:mm:ss") ----------設(shè)置你想要的格式,hh與HH的區(qū)別:分別表示12小時(shí)制,24小時(shí)制
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate* date = [formatter dateFromString:formatTime]; //------------將字符串按formatter轉(zhuǎn)成nsdate
    
    //時(shí)間轉(zhuǎn)時(shí)間戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]*1000] integerValue];
    
    NSLog(@"將某個(gè)時(shí)間轉(zhuǎn)化成 時(shí)間戳timeSp:%ld",(long)timeSp); //時(shí)間戳的值
    
    return timeSp;
    
}

//獲取當(dāng)前時(shí)間戳 (以毫秒為單位)
//返回值格式:1555642454396
//這里注意要獲取完全部數(shù)值后在(long)強(qiáng)轉(zhuǎn), 如果(long)[datenow timeIntervalSince1970]*1000 這么寫是先強(qiáng)轉(zhuǎn)在*1000,就是秒級(jí)別的數(shù)據(jù)了
- (NSString *)nowTimeSwitchTimestamp{
    
    NSDate *datenow = [NSDate date];//現(xiàn)在時(shí)間
    
    //這里如果long不夠用,就用(long long)
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)([datenow timeIntervalSince1970]*1000)];  
    
    return timeSp;
}
最后編輯于
?著作權(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ù)。

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