iOS-時(shí)間與日期詳解

  • 時(shí)間戳:從1970年1月1號(hào) 00:00:00開始走過的毫秒數(shù)
  • 注意:1秒==1000毫秒
// 時(shí)間字符串 -> 時(shí)間戳
NSString *string = @"1745645645645";
NSTimeInterval second = string.longLongValue / 1000.0;

// 時(shí)間戳 -> NSDate *
NSDate *date = [NSDate dateWithTimeIntervalSince1970:second];
NSLog(@"%@", date);

注意:使用[NSCalendar currentCalendar],iOS8以后會(huì)有bug,要加判斷

// 判斷方法一:
// 宏書寫注意:建議不要全是小寫,要有大寫,否則書寫時(shí)候參數(shù)有ken'neng不提示
#define iOS(version) ([UIDevice currentDevice].systemVersion.doubleValue >= (version))

NSCalendar *calendar = nil;
if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
    calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
} else {
    calendar = [NSCalendar currentCalendar];
}

// 判斷方法二:
NSCalendar *calendar = nil;
// NSCalendar不提示這個(gè)respondsToSelector:方法,但是的確有這個(gè)方法
if ([NSCalendar respondsToSelector:@selector(calendarWithIdentifier:)]) {
    calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
} else {
    calendar = [NSCalendar currentCalendar];
}

1.NSDate的詳細(xì)介紹

1.通過date方法創(chuàng)建出來的對(duì)象,就是當(dāng)前時(shí)間對(duì)象;
NSDate *date = [NSDate date];
NSLog(@"now = %@", date);

2.獲取當(dāng)前所處時(shí)區(qū)
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSLog(@"now = %@", zone);

3.獲取當(dāng)前時(shí)區(qū)和指定時(shí)間差
NSInteger seconds = [zone secondsFromGMTForDate:date];
NSLog(@"seconds = %lu", seconds);

NSDate *nowDate = [date dateByAddingTimeInterval:seconds];
NSLog(@"nowDate = %@", nowDate);

4.獲取當(dāng)前時(shí)間  NSDate --> NSString
NSDate *date = [NSDate date];

創(chuàng)建一個(gè)時(shí)間格式化對(duì)象
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

按照什么樣的格式來格式化時(shí)間
formatter.dateFormat = @"yyyy年MM月dd日 HH時(shí)mm分ss秒 Z";
formatter.dateFormat = @"yyyy/MM/dd HH/mm/ss Z";
formatter.dateFormat = @"MM-dd-yyyy HH-mm-ss";

NSString *res = [formatter stringFromDate:date];

2.字符串轉(zhuǎn)時(shí)間

    // 時(shí)間字符串
    NSString *str = @"2014-03-11 06:44:11 +0800";

    // 1.創(chuàng)建一個(gè)時(shí)間格式化對(duì)象
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    // 2.格式化對(duì)象的樣式/z大小寫都行/格式必須嚴(yán)格和字符串時(shí)間一樣
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";

    // 3.利用時(shí)間格式化對(duì)象讓字符串轉(zhuǎn)換成時(shí)間 (自動(dòng)轉(zhuǎn)換0時(shí)區(qū)/東加西減)
    NSDate *date = [formatter dateFromString:str];

    NSLog(@"%@",date);

3.時(shí)間轉(zhuǎn)換成字符串

    NSDate *now = [NSDate date];

    // 1.創(chuàng)建一個(gè)時(shí)間格式化對(duì)象
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    // 2.設(shè)置時(shí)間格式化對(duì)象的樣式
    formatter.dateFormat = @"yyyy年MM月dd日 HH時(shí)mm分ss秒 +0800";

    // 3.利用時(shí)間格式化對(duì)象對(duì)時(shí)間進(jìn)行格式化
    NSString *str = [formatter stringFromDate:now];

    NSLog(@"%@",str);

4.利用日歷比較兩個(gè)時(shí)間的差值

     // 時(shí)間字符串
    NSString *str = @"2012-03-11 06:44:11 +0800";

    // 1.創(chuàng)建一個(gè)時(shí)間格式化對(duì)象
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    // 2.格式化對(duì)象的樣式/z大小寫都行/格式必須嚴(yán)格和字符串時(shí)間一樣
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss Z";

    // 3.字符串轉(zhuǎn)換成時(shí)間/自動(dòng)轉(zhuǎn)換0時(shí)區(qū)/東加西減
    NSDate *date = [formatter dateFromString:str];
    NSDate *now = [NSDate date];
  
   // 注意獲取calendar,應(yīng)該根據(jù)系統(tǒng)版本判斷
    NSCalendar *calendar = [NSCalendar currentCalendar];

    NSCalendarUnit type = NSCalendarUnitYear |
    NSCalendarUnitMonth |
    NSCalendarUnitDay |
    NSCalendarUnitHour |
    NSCalendarUnitMinute |
    NSCalendarUnitSecond;
    
    // 4.獲取了時(shí)間元素
    NSDateComponents *cmps = [calendar components:type fromDate:date toDate:now options:0];

    NSLog(@"%ld年%ld月%ld日%ld小時(shí)%ld分鐘%ld秒鐘", cmps.year, cmps.month, cmps.day, cmps.hour, cmps.minute, cmps.second);

5.日期比較

// 時(shí)間字符串
NSString *createdAtString = @"2015-11-20 11:10:05";
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSDate *createdAtDate = [fmt dateFromString:createdAtString];

// 手機(jī)當(dāng)前時(shí)間
NSDate *nowDate = [NSDate date];

/**
 NSComparisonResult的取值
 NSOrderedAscending = -1L, // 升序, 越往右邊越大
 NSOrderedSame,  // 相等
 NSOrderedDescending // 降序, 越往右邊越小
 */
// 獲得比較結(jié)果(誰大誰小)
NSComparisonResult result = [nowDate compare:createdAtDate];
if (result == NSOrderedAscending) { // 升序, 越往右邊越大
    NSLog(@"createdAtDate > nowDate");
} else if (result == NSOrderedDescending) { // 降序, 越往右邊越小
    NSLog(@"createdAtDate < nowDate");
} else {
    NSLog(@"createdAtDate == nowDate");
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

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