一文搞懂 iOS 時(shí)間戳和日期轉(zhuǎn)化

概念或者基礎(chǔ)

1.時(shí)間戳是指1970年1月1日0時(shí)0分0秒到當(dāng)前時(shí)間的秒數(shù)。注意這里的當(dāng)前時(shí)間是指UTC+0時(shí)間,iOS時(shí)間戳是10位,服務(wù)器時(shí)間戳13位。
2.GMT和UTC, UTC協(xié)調(diào)世界時(shí),全世界通用的時(shí)間標(biāo)準(zhǔn),GMT可以粗暴認(rèn)為就是UTC, 北京時(shí)間 = UTC+8 = GMT+8
3.UTC+8 指的是北京時(shí)間,在UTC+0的時(shí)間的基礎(chǔ)上偏移了8個(gè)小時(shí),西8區(qū)是UTC-8
4.[NSDate date] 得到的是UTC+0時(shí)間
5.NSDateFormatter 默認(rèn)不設(shè)置時(shí)區(qū),拿到的是當(dāng)前手機(jī)時(shí)區(qū)的時(shí)間,如果定死了timeZone的時(shí)區(qū),那就不管在哪個(gè)地方時(shí)間都不會(huì)變,通過修改timeZone可以拿到所有地區(qū)時(shí)間
6.時(shí)間傳遞統(tǒng)一時(shí)間戳,服務(wù)器客戶端拿到的都是UTC+0的時(shí)間,通過這個(gè)時(shí)間戳轉(zhuǎn)化為UTC+0的NSDate對(duì)象,通過NSDateFormatter拿到指定時(shí)區(qū)的時(shí)間字符串。

獲取時(shí)區(qū)時(shí)間字符串

  NSDate *date = [NSDate date];
  NSDateFormatter *formatter = [[NSDateFormatter alloc] init];//最結(jié)尾的Z表示的是時(shí)區(qū),零時(shí)區(qū)表示+0000,東八區(qū)表示+0800
  [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//使用formatter轉(zhuǎn)換后的date字符串變成了當(dāng)前時(shí)區(qū)的時(shí)間
  formatter.timeZone = [NSTimeZone systemTimeZone]; //拿到的是當(dāng)前時(shí)區(qū)的時(shí)間,這行代碼可以不要,默認(rèn)的timeZone就是當(dāng)前時(shí)區(qū),通過timeZone可以拿到任意時(shí)區(qū)的時(shí)間字符串
  NSString *dateStr = [formatter stringFromDate:date];

時(shí)間字符串轉(zhuǎn)化為NSDate

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
 NSString *dateStr = @"2016-12-07 14:06:24";//默認(rèn)是當(dāng)?shù)貢r(shí)區(qū)時(shí)間,如果指定了某個(gè)時(shí)區(qū),拿到的就是指定時(shí)區(qū)的時(shí)間  
 [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
 NSDate *newDate = [formatter dateFromString:dateStr];

時(shí)間戳和日期相互轉(zhuǎn)化

  NSDate *date = [NSDate date];
  NSTimeInterval interval = [date timeIntervalSince1970];//日期轉(zhuǎn)化為時(shí)間戳
  NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:interval]; //時(shí)間戳轉(zhuǎn)化為日期

NSDate 轉(zhuǎn)化為UTC字符串

   // 方法1 是用一個(gè)NSDate對(duì)象,用兩個(gè)formatter, 方法二用兩個(gè)NSDate對(duì)象一個(gè)formatter
    NSDate *date = [NSDate date];//獲取當(dāng)前時(shí)區(qū)時(shí)間
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    formatter.timeZone = [NSTimeZone defaultTimeZone];
    
    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
    [formatter2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    formatter2.timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
    NSString *realDate = [formatter stringFromDate:date];
    NSString *utcString = [formatter2 stringFromDate:date];

時(shí)間戳轉(zhuǎn)化為字符串日期

- (NSString *)convertToTimeStringWithTimeInterval:(NSTimeInterval)timeInterval{    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];     
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];    
    NSString *dateStsring = [dateFormatter stringFromDate:date];             
    return dateStsring;
}

時(shí)間字符串轉(zhuǎn)化為時(shí)間戳

- (NSTimeInterval)convertToTimeIntervalWithTimeString:(NSString *)timeString{    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//不指定時(shí)區(qū)就是默認(rèn)是當(dāng)前時(shí)區(qū)    
    NSDate *date = [dateFormatter dateFromString:timeString];   
    NSTimeInterval timeInterval = [date timeIntervalSince1970];   
    return timeInterval;
}

獲取年月日

  NSDate *date = [NSDate date];
  NSDateComponents *components = [[NSCalendar currentCalendar] components:    (NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay) fromDate:date];
  NSInteger day = components.day;
  NSInteger day = components.month;
  NSInteger day = components.year;

獲取當(dāng)前時(shí)區(qū)當(dāng)天0點(diǎn)時(shí)間

  NSDate *nowDate = [NSDate date];
  NSCalendar *calendar = [NSCalendar currentCalendar];
  NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:nowDate];//從日歷中獲取年月日去掉時(shí)分秒獲取
  NSDate *zeroHourDate = [calendar dateFromComponents:components];//從日歷中拿到0點(diǎn)

獲取當(dāng)前時(shí)區(qū)的偏移量

  NSInteger seconds = [[NSTimeZone localTimeZone] secondsFromGMT];//從當(dāng)前時(shí)區(qū)獲取偏移的秒數(shù)
  CGFloat timeZone = seconds/3600.0f;//轉(zhuǎn)化為小時(shí)即為偏移量, UTC+8的偏移量就是8,UTC+0就是0,如果是西8區(qū)即UTC-8,拿到的偏移量就是-8。

獲取東8區(qū)16點(diǎn)時(shí)間

//思路東8區(qū)18點(diǎn)就是在當(dāng)?shù)貢r(shí)區(qū)的基礎(chǔ)上偏移8個(gè)小時(shí)即可,考慮一種情況如果當(dāng)?shù)貢r(shí)區(qū)偏移加上8小時(shí)偏移小于0.這個(gè)時(shí)候獲取的時(shí)間是昨天的東8區(qū)16點(diǎn),這個(gè)時(shí)候加上24點(diǎn),可以拿到當(dāng)天的東8區(qū)16點(diǎn)    
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear |
                                                        NSCalendarUnitMonth |
                                                        NSCalendarUnitDay
                                               fromDate:nowDate];
    //獲取今天0點(diǎn)時(shí)間
NSDate *zeorDate = [calendar dateFromComponents:components];
CGFloat offset = 8 * 3600 + [[NSTimeZone localTimeZone] secondsFromGMT];
if (offset < 0) {
    offset = offset + 24 * 3600;
}
NSDate *newDate = [zeorDate dateByAddingTimeInterval:offset];
 

獲取當(dāng)前時(shí)間未來某一天時(shí)間

    NSTimeInterval intervals = 24 * 3600 * dayNum;
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:intervals];

獲取指定日期未來X時(shí)間

    NSDate *date; //未來日期
    NSTimeInterval intervals = 24 * 3600 * X;//X代表天數(shù) date指定日期
    NSDate *lastDay = [NSDate dateWithTimeInterval:intervals
                                         sinceDate:date];

DateFormat 格式

 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
 dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";//蘋果官方網(wǎng)站例子 @"yyyy-MM-dd'T'HH:mm:ssZZZZZ"
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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