程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> NSDate

NSDate

編輯:關於C++
//
//  main.m
//  OC05-task-06
//
//  Created by Xin the Great on 15-1-26.
//  Copyright (c) 2015年 Xin the Great. All rights reserved.
//

#import 

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        //////////////////////NSDate --- 日期//////////////////////
        
        //獲取當前系統的時間   標准時間 GMT  格林尼治時間
        NSDate *date = [NSDate date];
        NSLog(@"date is %@", date);
        
        NSDate *date1 = [[NSDate alloc] init];
        NSLog(@"date1 is %@", date1);
        
        //獲取時間戳   以秒為單位
        NSTimeInterval time1970 = [date timeIntervalSince1970];
        NSLog(@"time1970 is %.1f", time1970);
        
        NSTimeInterval time2001 = [date timeIntervalSinceReferenceDate];
        NSLog(@"time2001 is %.1f", time2001);
        
        NSTimeInterval time = [date timeIntervalSinceNow];
        NSLog(@"time is %.1f", time);
        
        //獲取昨天的時間
        NSTimeInterval second = 24 * 60 * 60;
        NSDate *yesterDayDate = [[NSDate alloc] initWithTimeIntervalSinceNow:-second];
        NSLog(@"yesterDayDate is %@",yesterDayDate);
        
        //獲取明天的時間
        NSDate *tomorrowDayDate = [NSDate dateWithTimeInterval:second sinceDate:[NSDate date]];
        NSLog(@"tomorrowDayDate is %@", tomorrowDayDate);
        
        //獲得未來的某一個時間
        NSDate *future = [NSDate distantFuture];
        NSLog(@"future is %@", future);
        
        //獲得古代的某一個時間
        NSDate *past = [NSDate distantPast];
        NSLog(@"past is %@", past);
        
        //日期的比較
//        BOOL isTure = [date isEqualToDate:date1];
//        NSLog(@"isTure is %d", isTure);
        
        //返回兩個時間比較早的那個時間
        NSDate *earlierDate = [tomorrowDayDate earlierDate:future];
        NSLog(@"earlierDate is %@", earlierDate);
        
        //返回兩個時間比較晚的那個時間
        NSDate *later = [tomorrowDayDate laterDate:future];
        NSLog(@"later is %@", later);
        
        
        //將時間戳轉換成字符串
        NSString *str = @"123456789";
        NSTimeInterval second2 = [str doubleValue];
        NSDate *date3 = [NSDate dateWithTimeIntervalSince1970:second2];
        NSLog(@"date3 is %@", date3);
        
        //格式化日期類
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        [df setDateFormat:@"yyyy年MM月dd日 HH小時mm分鐘ss秒 ZZZZ"];
        
        //將日期按照格式化日期類轉換為字符串
        NSString *str2 = [df stringFromDate:date3];
        NSLog(@"str2 is %@", str2);
        
        //通過字符串轉換為date
        NSDate *date4 = [df dateFromString:str2];
        NSLog(@"date4 is %@", date4);
        
    }
    return 0;
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved