新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
iOS中時(shí)間與時(shí)間戳的相互轉(zhuǎn)化實(shí)例代碼-創(chuàng)新互聯(lián)
本人搜索了很多關(guān)于iOS中時(shí)間與時(shí)間戳的相互轉(zhuǎn)化的資料,下面我來記錄一下,有需要了解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; }
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
分享文章:iOS中時(shí)間與時(shí)間戳的相互轉(zhuǎn)化實(shí)例代碼-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://www.ef60e0e.cn/article/gddcg.html