ios時(shí)間戳轉(zhuǎn)換成時(shí)間

設(shè)置時(shí)間顯示格式:
NSString* timeStr = @"2011-01-26 17:40:50";
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[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í)很重要
//例如你在國內(nèi)發(fā)布信息,用戶在國外的另一個(gè)時(shí)區(qū),你想讓用戶看到正確的發(fā)布時(shí)間就得注意時(shí)區(qū)設(shè)置,時(shí)間的換算.
//例如你發(fā)布的時(shí)間為2010-01-26 17:40:50,那么在英國愛爾蘭那邊用戶看到的時(shí)間應(yīng)該是多少呢?
//他們與我們有7個(gè)小時(shí)的時(shí)差,所以他們那還沒到這個(gè)時(shí)間呢...那就是把未來的事做了

NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];

NSDate* date = [formatter dateFromString:timeStr]; //------------將字符串按formatter轉(zhuǎn)成nsdate
       
NSDate *datenow = [NSDate date];//現(xiàn)在時(shí)間,你可以輸出來看下是什么格式

NSString *nowtimeStr = [formatter stringFromDate:datenow];//----------將nsdate按formatter格式轉(zhuǎn)成nsstring

時(shí)間轉(zhuǎn)時(shí)間戳的方法:
NSString *timeSp = [NSString stringWithFormat:@"%d", (long)[datenow timeIntervalSince1970]];
NSLog(@"timeSp:%@",timeSp); //時(shí)間戳的值
時(shí)間戳轉(zhuǎn)時(shí)間的方法
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:1296035591];
NSLog(@"1296035591 = %@",confromTimesp);
NSString confromTimespStr = [formatter stringFromDate:confromTimesp];
NSLog(@"confromTimespStr = %@",confromTimespStr);
時(shí)間戳轉(zhuǎn)時(shí)間的方法:
NSDateFormatter
formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"yyyyMMddHHMMss"];
NSDate *date = [formatter dateFromString:@"1283376197"];
NSLog(@"date1:%@",date);
[formatter release];

最后編輯于
?著作權(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)容

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