iOS-OC-基礎-NSDate常用方法


NSDate常用方法

    /*=============================NSDate日期類的使用=============================*/
    
    // 獲取當前時間,獲得的時0市區的時間跟北京時間相差8小時
    NSDate *currentDate = [NSDate date]; // GMT
    NSLog(@"currentDate :%@",currentDate);
    
    //timeIntervalSince1970 到1970-1-1的秒數,也叫時間戳(NSTimeInterval)
    NSTimeInterval interval1970 = [currentDate timeIntervalSince1970];
    NSLog(@"interval = %lf",interval1970);
    
    // timeIntervalSinceReferenceDate 到2001-1-1 的秒數
    NSTimeInterval interval2001 = [currentDate timeIntervalSinceReferenceDate];
    NSLog(@"interval2001 = %lf",interval2001);
    
    // timeIntervalSinceNow 距當前時間的秒數
    NSTimeInterval intervalNow = [currentDate timeIntervalSinceNow];
    NSLog(@"intervalNow = %lf",intervalNow);
    
    
    
    NSTimeInterval hour = 60 * 60;
    // 一小時后
    NSDate *h1 = [currentDate dateByAddingTimeInterval:hour];
    NSLog(@"h1 :%@",h1);
    
    // 一小時前
    NSDate *h2 = [currentDate dateByAddingTimeInterval:-hour];
    NSLog(@"h2 :%@",h2);
    
    // 計算北京時區時間,(使用系統當前的時區:systemTimeZone)
    NSTimeInterval inter = [[NSTimeZone systemTimeZone] secondsFromGMT];
    NSDate *bjDate = [currentDate dateByAddingTimeInterval:inter];
    NSLog(@"bjDate :%@",bjDate);
    
    
    
    NSTimeInterval day = 24 * 60 * 60;
    // 方式一:
    //        NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow:-day];
    // 方式二:
    
    // 昨天
    NSDate *yesterday = [bjDate dateByAddingTimeInterval:-day];
    NSLog(@"yesterday:%@",yesterday);
    
    // 今天
    NSDate *today = [bjDate dateByAddingTimeInterval:0];
    NSLog(@"today:%@",today);
    
    // 明天
    NSDate *tomorrow = [bjDate dateByAddingTimeInterval:day];
    NSLog(@"tomorrow:%@",tomorrow);
    
    // isEqualToDate 兩個日期的比較
    if ([yesterday isEqualToDate:tomorrow]) {
        
        NSLog(@"兩個日期相同");
        
    } else {
        
        NSLog(@"兩個日期不相同");
        
    }
    
    // compare 兩個日期的比較
    NSComparisonResult result = [yesterday compare:tomorrow];
    if (result == NSOrderedAscending) {
        
        NSLog(@"日期升序");
        
    } else if(result == NSOrderedSame) {
        
        NSLog(@"兩個日期相同");
        
    } else if(result == NSOrderedDescending) {
        
        NSLog(@"兩個日期降序");
    }
    
    //distantFuture 未來的一個時間 4001-01-01 00:00:00
    NSDate *future = [NSDate distantFuture];
    NSLog(@"future :%@",future);
    //distantPast 遠古的一個時間 0001-12-30 00:00:00
    NSDate *past = [NSDate distantPast];
    NSLog(@"past :%@",past);
    
    
    // dateWithTimeIntervalSince1970 將時間戳轉為日期類型
    NSString *time = @"34567898";
    NSDate *timeDate = [NSDate dateWithTimeIntervalSince1970:[time doubleValue]];
    NSLog(@"timeDate :%@",timeDate);
    
    NSDate *now = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年MM月dd日 HH:mm:ss"];
    
    // 2014年10月15日 16:35:42
    // stringFromDate 將日期類型格式化,轉為NSString 類型
    NSString *current = [formatter stringFromDate:now];
    NSLog(@"current:%@",current);
    
    //dateFromString 將時間字符串轉化為日期類型,
    [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; // 默認使用0時區,所以需要時區的轉換
    NSDate *nowDate = [formatter dateFromString:current];
    NSLog(@"nowDate :%@",nowDate);

本文GitHub地址:https://github.com/zhangkiwi/iOS_SN_NSDate


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM