欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS中時(shí)間與時(shí)間戳的相互轉(zhuǎn)化實(shí)例代碼

 更新時(shí)間:2017年03月10日 11:56:45   作者:張無(wú)忌!  
這篇文章主要介紹了iOS中時(shí)間與時(shí)間戳的相互轉(zhuǎn)化實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。

本人搜索了很多關(guān)于iOS中時(shí)間與時(shí)間戳的相互轉(zhuǎn)化的資料,下面我來(lái)記錄一下,有需要了解iOS中時(shí)間與時(shí)間戳的相互轉(zhuǎn)化的朋友可參考。希望此文章對(duì)各位有所幫助。

//獲取當(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è)對(duì)于時(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;

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論