C++ 中時(shí)間與時(shí)間戳的轉(zhuǎn)換實(shí)例詳解
文安ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
// 設(shè)置時(shí)間顯示格式: NSString *timeStr = @"2011-01-26 17:40:50"; 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í)很重要 //例如你在國內(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 NSLog(@"date = %@", date); NSDate *datenow = [NSDate date];//現(xiàn)在時(shí)間,你可以輸出來看下是什么格式 NSLog(@"datenow = %@", datenow); NSString *nowtimeStr = [formatter stringFromDate:datenow];//----------將nsdate按formatter格式轉(zhuǎn)成nsstring,nsstring會顯示與當(dāng)前的時(shí)間吻合的串 NSLog(@"nowtimeStr = %@", nowtimeStr); // 時(shí)間轉(zhuǎn)時(shí)間戳的方法: NSString *timeSp = [NSString stringWithFormat:@"%ld", (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* formatter1 = [[NSDateFormatter alloc] init]; [formatter1 setDateStyle:NSDateFormatterMediumStyle]; [formatter1 setTimeStyle:NSDateFormatterShortStyle]; [formatter1 setDateFormat:@"yyyyMMddHHMMss"]; NSDate *date1 = [formatter1 dateFromString:@"1283376197"]; NSLog(@"date1:%@",date1); 當(dāng)前時(shí)間是:14:41:57
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!