iOS - OC NSDate 時間


前言

  • NSDate

    	@interface NSDate : NSObject <NSCopying, NSSecureCoding>
    
    • NSDate 用來表示公歷的 GMT 時間(格林威治時間)。是獨立與任何歷法的,它只是時間相對於某個時間點的時間差;NSDate 是進行日歷計算的基礎。
  • NSDateComponents

    	@interface NSDateComponents : NSObject <NSCopying, NSSecureCoding>
    
    • NSDateComponents 封裝了具體年月日、時秒分、周、季度等。將時間表示成適合人類閱讀和使用的方式,通過 NSDateComponents 可以快速而簡單地獲取某個時間點對應的“年”,“月”,“日”,“時”,“分”,“秒”,“周”等信息。當然一旦涉及了年月日時分秒就要和某個歷法綁定,因此 NSDateComponents 必須和 NSCalendar 一起使用,默認為公歷。NSDateComponents 除了像上面說的表示一個時間點外,還可以表示時間段,例如:兩周,三個月,20年,7天,10分鍾,50秒等等。時間段用於日歷的計算,例如:獲取當前歷法下,三個月前的某個時間點。可以說,要獲取某個時間點在某個歷法下的表示,需要 NSDateComponents; 要計算當前時間點在某個歷法下對應的一個時間段前或后的時間點,需要 NSDateComponents。NSDateComponents 返回的 day, week, weekday, month, year 這一類數據都是從 1 開始的。因為日歷是給人看的,不是給計算機看的,從 0 開始就是個錯誤。
  • NSDateFormatter

    	@interface NSDateFormatter : NSFormatter
    
    • NSDateFomatter 表示的時間默認以公歷(即陽歷)為參考,可以通過設置 calendar 屬性變量獲得特定歷法下的時間表示。

1、NSDate 的創建

	// 對象方法
	    
		// 當前時間值,GMT 時間
		NSDate *date1 = [[NSDate alloc] init];
		    
		// 當前時間加 n 秒后的值
		NSDate *date2 = [[NSDate alloc] initWithTimeIntervalSinceNow:10];
		    
		// 某個時間增加 n 秒后的值
		NSDate *date3 = [[NSDate alloc] initWithTimeInterval:10 sinceDate:date2];
		    
		// 從 1970-01-01 00:00:00 (GMT) 增加 n 秒后的值
		NSDate *date4 = [[NSDate alloc] initWithTimeIntervalSince1970:3600];
		    
		// 從 2001-01-01 00:00:00 (GMT) 增加 n 秒后的值
		NSDate *date5 = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:10];
	    
	// 類方法
	    
		// 當前時間值,GMT 時間
		NSDate *date6 = [NSDate date];
		    
		// 當前時間加 n 秒后的值
		NSDate *date7 = [NSDate dateWithTimeIntervalSinceNow:10];
		    
		// 某個時間增加 n 秒后的值
		NSDate *date8 = [NSDate dateWithTimeInterval:10 sinceDate:date2];
		    
		// 從 1970-01-01 00:00:00 (GMT) 增加 n 秒后的值
		NSDate *date9 = [NSDate dateWithTimeIntervalSince1970:3600];
		    
		// 從 2001-01-01 00:00:00 (GMT) 增加 n 秒后的值
		NSDate *date10 = [NSDate dateWithTimeIntervalSinceReferenceDate:10];
		    
		// 未來某一個隨機時間
		NSDate *date11 = [NSDate distantFuture];
		    
		// 過去某一個隨機時間
		NSDate *date12 = [NSDate distantPast];
		    
		// 指定時間間隔
		/*
			某個時間增加 n 秒后的值
		*/
		NSDate *date13 = [date6 dateByAddingTimeInterval:20];

2、NSDate 時間間隔的計算

	NSDate *date1 = [NSDate date];
	NSDate *date2 = [date1 dateByAddingTimeInterval:20];
	    
	// 實例保存的時間 與 當前時間 的時間間隔,單位 秒
	NSTimeInterval interval1 = [date2 timeIntervalSinceNow];
	    
	// 實例保存的時間 與 指定時間 的時間間隔,單位 秒
	NSTimeInterval interval2 = [date2 timeIntervalSinceDate:date1];
	    
	// 實例保存的時間 與 1970-01-01 00:00:00 (GMT) 的時間間隔,單位 秒
	NSTimeInterval interval3 = [date1 timeIntervalSince1970];
	    
	// 實例保存的時間 與 2001-01-01 00:00:00 (GMT) 的時間間隔,單位 秒
	NSTimeInterval interval4 = [date1 timeIntervalSinceReferenceDate];
	    
	// 當前時間 與 2001-01-01 00:00:00 (GMT) 的時間間隔,單位 秒
	NSTimeInterval interval5 = [NSDate timeIntervalSinceReferenceDate];

3、NSDate 時間的比較

	NSDate *date1 = [NSDate date];
	NSDate *date2 = [date1 dateByAddingTimeInterval:10];
	    
	// isEqualToDate
	    
		// 判斷兩個時間是否相等
		BOOL bl = [date1 isEqualToDate:date2];
	    
	// compare
	    
		// 比較兩個時間大小
		NSComparisonResult result = [date1 compare:date2];
	  
	// earlierDate
	    
		// 比較兩個時間,返回 較早的時間
		NSDate *date3 = [date1 earlierDate:date2];
		    
	// laterDate
	    
		// 比較兩個時間,返回 較晚的時間
		NSDate *date4 = [date1 laterDate:date2];

4、NSDateComponents 的創建

	// 由手動設置創建
	NSDateComponents *compt1 = [[NSDateComponents alloc] init];
	    
	// 日歷
	[compt1 setCalendar:[NSCalendar currentCalendar]];
	
	// 時區
	[compt1 setTimeZone:[NSTimeZone systemTimeZone]];
	
	// 紀元
	[compt1 setEra:1];
	
	// 年
	[compt1 setYear:2016];
	
	// 月
	[compt1 setMonth:3];
	
	// 日
	[compt1 setDay:1];
	
	// 時
	[compt1 setHour:00];
	
	// 分
	[compt1 setMinute:10];
	
	// 秒
	[compt1 setSecond:55];
	
	// 微妙
	[compt1 setNanosecond:280];
	
	// 刻鍾
	[compt1 setQuarter:0];
	
	// 周幾
	[compt1 setWeekday:3];
	
	// 指定日期為本月的第幾個星期幾
	[compt1 setWeekdayOrdinal:1];
	
	// 指定日期為本月的第幾周
	[compt1 setWeekOfMonth:1];
	
	// 指定日期為本年的第幾周
	[compt1 setWeekOfYear:10];
	
	//
	[compt1 setYearForWeekOfYear:2016];
    
	//  由已知 date 創建
	/*
		只有明確指定了 unitFlags,NSDateComponents 相應的那一部分才有值
	*/
	NSDateComponents *compt2 = [[NSCalendar currentCalendar] components:NSCalendarUnitEra
	                                                                  | NSCalendarUnitYear
	                                                                  | NSCalendarUnitMonth
	                                                                  | NSCalendarUnitDay
	                                                                  | NSCalendarUnitHour
	                                                                  | NSCalendarUnitMinute
	                                                                  | NSCalendarUnitSecond
	                                                                  | NSCalendarUnitNanosecond
	                                                                  | NSCalendarUnitQuarter
	                                                                  | NSCalendarUnitWeekday
	                                                                  | NSCalendarUnitWeekdayOrdinal
	                                                                  | NSCalendarUnitWeekOfMonth
	                                                                  | NSCalendarUnitWeekOfYear
	                                                                  | NSCalendarUnitYearForWeekOfYear
	                                                                  | NSCalendarUnitCalendar
	                                                                  | NSCalendarUnitTimeZone
												    
	                                                           fromDate:[NSDate date]];

5、NSDateComponents 時間間隔的計算

	NSDate *date1 = [NSDate date];
	NSDate *date2 = [NSDate dateWithTimeInterval:5*60*60+75 sinceDate:date1];
	    
	NSDateComponents *compt = [[NSCalendar currentCalendar] components:NSCalendarUnitMinute 
	                                                                 | NSCalendarUnitSecond 
	                                                          fromDate:date1 
	                                                            toDate:date2 
	                                                           options:0];

6、NSDateComponents 與 NSDate 的相互轉換

	NSCalendar *calendar = [NSCalendar currentCalendar];
	    
	// NSDate 轉 NSDateComponents
	
		NSDateComponents *compt = [calendar components:NSCalendarUnitYear 
		                                             | NSCalendarUnitMonth 
		                                             | NSCalendarUnitDay 
		                                      fromDate:[NSDate date]];
	    
	// NSDateComponents 轉 NSDate
	    
		// 轉換時間時,使用默認的時區 GMT 時區
		NSDate *date = [calendar dateFromComponents:compt];                                                 
		    
		// 得到本地時間,避免時區問題
		date = [date dateByAddingTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMTForDate:date]];    

7、NSDateComponents 與 NSDate 的計算

	NSDateComponents *compt = [[NSDateComponents alloc] init];
	[compt setDay:25];
	[compt setHour:4];
	[compt setMinute:66];
	    
	// NSDate 加上 NSDateComponents 表示的一段時間
	NSDate *date = [[NSCalendar currentCalendar] dateByAddingComponents:compt toDate:[NSDate date] options:0];
	    
	// 得到本地時間,避免時區問題
	date = [date dateByAddingTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMTForDate:date]];    

8、NSDateFormatter 的時間格式化

	G -- 紀元				     一般會顯示公元前(BC)和公元(AD)
	y -- 年					  假如是2013年,那么 yyyy=2013,yy=13
	M -- 月					  假如是3月,那么 M=3,MM=03,MMM=Mar,MMMM=March;假如是11月,那么M=11,MM=11,
	                            MMM=Nov,MMMM=November
	w -- 一年中的第幾周		 假如是1月8日,那么 w=2(這一年的第二個周)
	W -- 一個月中的第幾周		與日歷排列有關,假如是2013年4月21日,那么 W=4(這個月的第四個周)
	F -- 月份包含的周			  與日歷排列無關,和上面的 W 不一樣,F 只是單純以7天為一個單位來統計周,例如7號一定是第一個周,
	                           15號一定是第三個周,與日歷排列無關。
	D -- 一年中的第幾天		 假如是1月20日,那么 D=20(這一年的第20天);假如是2月25日,那么 D=31+25=56(這一年的第56天)
	d -- 一個月中的第幾天		假如是5號,那么 d=5,dd=05   假如是15號,那么 d=15,dd=15
	E -- 星期幾				假如是星期五,那么 E=Fri,EEEE=Friday
	a -- 上午(AM)/下午(PM)
	H -- 24小時制				顯示為0--23,假如是午夜00:40,那么 H=0:40,HH=00:40
	h -- 12小時制				顯示為1--12,假如是午夜00:40,那么 h=12:40
	K -- 12小時制				顯示為0--11,假如是午夜00:40,那么 K=0:40,KK=00:40
	k -- 24小時制				顯示為1--24,假如是午夜00:40,那么 k=24:40
	m -- 分鍾				     假如是5分鍾,那么 m=5,mm=05;假如是45分鍾,那么 m=45,mm=45
	s -- 秒					  假如是5秒鍾,那么 s=5,ss=05;假如是45秒鍾,那么 s=45,ss=45
	S -- 毫秒				     一般用 SSS 來顯示
	z -- 時區				     表現形式為 GMT+08:00
	Z -- 時區				     表現形式為 +0800

	// 使用 NSDateFormatter 轉換時間字符串時,默認的時區是系統時區,例如在中國一般都是北京時間(+8),
	// 如果直接轉換會導致結果相差8小時,所以一般的做法是先指定時區為GMT標准時間再轉換。
	
	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
	    
	// 設置時區,不設置時默認的時區是系統時區(GMT+8)
	[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
	    
	// 設置日期格式,以字符串表示的日期形式的格式
	[formatter setDateFormat:@"G yyyy-MM-dd E D F w W a z HH:mm:ss.SSS"];
	    
	// 格式化日期,GMT 時間,NSDate 轉 NSString
	NSString *str = [formatter stringFromDate:[NSDate date]];                       

9、1437494603 (秒) 格式 轉 YYYY-MM-dd HH:mm:ss 格式

	// 計算日期
	NSDate *date = [NSDate dateWithTimeIntervalSince1970:1437494603];               
	    
	// 創建時間戳
	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];                    
	    
	// 設置日期格式,以字符串表示的日期形式的格式
	[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];                               
	    
	// 轉換成指定的格式
	NSString *string = [formatter stringFromDate:date];                             

10、NSDate 與 NSString 的相互轉換

	NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
	    
	// 設置時區 GMT
	[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
	    
	// 加入一些別的英文字符串時,需用單引號來引入
	[formatter setDateFormat:@"yyyy年MM月dd日 HH點mm分ss秒 'iOS Date Test'"];
	    
	// NSString 轉 NSDate
	NSDate *date = [formatter dateFromString:@"2016年01月12日 1點8分50秒"];
		
	// NSDate 轉 NSString
	NSString *str = [formatter stringFromDate:date];

11、時區差值轉換

	// 得到當前時間(世界標准時間 UTC/GMT)
	NSDate *date = [NSDate date];
	   	
	// 設置系統時區為本地時區
	NSTimeZone *zone = [NSTimeZone systemTimeZone];
	    
	// 計算本地時區與 GMT 時區的時間差
	NSInteger interval = [zone secondsFromGMT];
	  	
	// 在 GMT 時間基礎上追加時間差值,得到本地時間
	date = [date dateByAddingTimeInterval:interval];


免責聲明!

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



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