iOS中時(shí)間與時(shí)間戳的相互轉(zhuǎn)化實(shí)例代碼
本人搜索了很多關(guān)于iOS中時(shí)間與時(shí)間戳的相互轉(zhuǎn)化的資料,下面我來記錄一下,有需要了解iOS中時(shí)間與時(shí)間戳的相互轉(zhuǎn)化的朋友可參考。希望此文章對各位有所幫助。
//獲取當(dāng)前系統(tǒng)時(shí)間的時(shí)間戳
#pragma mark - 獲取當(dāng)前時(shí)間的 時(shí)間戳
+(NSInteger)getNowTimestamp{
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è)對于時(shí)間的處理有時(shí)很重要
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate *datenow = [NSDate date];//現(xiàn)在時(shí)間
NSLog(@"設(shè)備當(dāng)前的時(shí)間:%@",[formatter stringFromDate:datenow]);
//時(shí)間轉(zhuǎn)時(shí)間戳的方法:
NSInteger timeSp = [[NSNumber numberWithDouble:[datenow timeIntervalSince1970]] integerValue];
NSLog(@"設(shè)備當(dāng)前的時(shí)間戳:%ld",(long)timeSp); //時(shí)間戳的值
return timeSp;
}
//將某個(gè)時(shí)間轉(zhuǎn)化成 時(shí)間戳
#pragma mark - 將某個(gè)時(shí)間轉(zhuǎn)化成 時(shí)間戳
+(NSInteger)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format]; //(@"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]] integerValue];
NSLog(@"將某個(gè)時(shí)間轉(zhuǎn)化成 時(shí)間戳&&&&&&&timeSp:%ld",(long)timeSp); //時(shí)間戳的值
return timeSp;
}
//將某個(gè)時(shí)間戳轉(zhuǎn)化成 時(shí)間
#pragma mark - 將某個(gè)時(shí)間戳轉(zhuǎn)化成 時(shí)間
+(NSString *)timestampSwitchTime:(NSInteger)timestamp andFormatter:(NSString *)format{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:format]; // (@"YYYY-MM-dd hh:mm:ss")----------設(shè)置你想要的格式,hh與HH的區(qū)別:分別表示12小時(shí)制,24小時(shí)制
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
[formatter setTimeZone:timeZone];
NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSLog(@"1296035591 = %@",confromTimesp);
NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
//NSLog(@"&&&&&&&confromTimespStr = : %@",confromTimespStr);
return confromTimespStr;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS中的應(yīng)用啟動(dòng)原理以及嵌套模型開發(fā)示例詳解
這篇文章主要介紹了iOS中的應(yīng)用啟動(dòng)原理以及嵌套模型開發(fā)示例詳解,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-12-12
iOS中解決Xcode9的Log日志無法輸出中文的問題小結(jié)
這篇文章主要介紹了iOS中解決Xcode9的Log日志無法輸出中文的問題小結(jié),需要的朋友可以參考下2017-11-11
Objective-C 代碼與Javascript 代碼相互調(diào)用實(shí)例
這篇文章主要介紹了Objective-C 代碼與Javascript 代碼相互調(diào)用實(shí)例的相關(guān)資料,現(xiàn)在的APP 應(yīng)用有時(shí)候會(huì)調(diào)用網(wǎng)頁上的內(nèi)容,為了增加用戶體驗(yàn),這里寫下個(gè)實(shí)例,需要的朋友可以參考下2016-10-10
iOS實(shí)現(xiàn)導(dǎo)航欄透明示例代碼
本篇文章主要介紹了iOS實(shí)現(xiàn)導(dǎo)航欄透明示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
iOS應(yīng)用設(shè)計(jì)模式開發(fā)中對簡單工廠和工廠方法模式的運(yùn)用
這篇文章主要介紹了iOS應(yīng)用設(shè)計(jì)模式開發(fā)中對簡單工廠和工廠方法模式的運(yùn)用,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03

