NSDate類用於保存時間值,同時提供了一些方法來處理一些基於秒級別時差(Time Interval)運算和日期之間的早晚比較等。
1. 創建或初始化可用以下方法
用於創建NSDate實例的類方法有
+ (id)date;
返回當前時間
+ (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;
返回以當前時間為基准,然后過了secs秒的時間
+ (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;
返回以2001/01/01 GMT為基准,然后過了secs秒的時間
+ (id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
返回以1970/01/01 GMT為基准,然后過了secs秒的時間
+ (id)distantFuture;
返回很多年以后的未來的某一天。
比如你需要一個比現在(Now)晚(大)很長時間的時間值,則可以調用該方法。測試返回了4000/12/31 16:00:00
+ (id)distantPast;
返回很多年以前的某一天。
比如你需要一個比現在(Now)早(小)大很長時間的時間值,則可以調用該方法。測試返回了公元前0001/12/31 17:00:00
用於創建NSDate實例的實例方法有
- (id)addTimeInterval:(NSTimeInterval)secs;
返回以目前的實例中保存的時間為基准,然后過了secs秒的時間
用於初始化NSDate實例的實例方法有
- (id)init;
初始化為當前時間。類似date方法
- (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;
初始化為以2001/01/01 GMT為基准,然后過了secs秒的時間。類似dateWithTimeIntervalSinceReferenceDate:方法
- (id)initWithTimeInterval:(NSTimeInterval)secs sinceDate:(NSDate *)refDate;
初始化為以refDate為基准,然后過了secs秒的時間
- (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;
初始化為以當前時間為基准,然后過了secs秒的時間
2. 日期之間比較可用以下方法
- (BOOL)isEqualToDate:(NSDate *)otherDate;
與otherDate比較,相同返回YES
- (NSDate *)earlierDate:(NSDate *)anotherDate;
與anotherDate比較,返回較早的那個日期
- (NSDate *)laterDate:(NSDate *)anotherDate;
與anotherDate比較,返回較晚的那個日期
- (NSComparisonResult)compare:(NSDate *)other;
該方法用於排序時調用:
. 當實例保存的日期值與anotherDate相同時返回NSOrderedSame
. 當實例保存的日期值晚於anotherDate時返回NSOrderedDescending
. 當實例保存的日期值早於anotherDate時返回NSOrderedAscending
3. 取回時間間隔可用以下方法
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate;
以refDate為基准時間,返回實例保存的時間與refDate的時間間隔
- (NSTimeInterval)timeIntervalSinceNow;
以當前時間(Now)為基准時間,返回實例保存的時間與當前時間(Now)的時間間隔
- (NSTimeInterval)timeIntervalSince1970;
以1970/01/01 GMT為基准時間,返回實例保存的時間與1970/01/01 GMT的時間間隔
- (NSTimeInterval)timeIntervalSinceReferenceDate;
以2001/01/01 GMT為基准時間,返回實例保存的時間與2001/01/01 GMT的時間間隔
+ (NSTimeInterval)timeIntervalSinceReferenceDate;
以2001/01/01 GMT為基准時間,返回當前時間(Now)與2001/01/01 GMT的時間間隔
4. 將時間表示成字符串
- (NSString *)description;
以YYYY-MM-DD HH:MM:SS ±HHMM的格式表示時間。
其中 "±HHMM" 表示與GMT的存在多少小時多少分鍾的時區差異。比如,若時區設置在北京,則 "±HHMM" 顯示為 "+0800"
//得到當前的日期
NSDate *date = [NSDate date];
NSLog(@"date:%@",date);
//得到(24 * 60 * 60)即24小時之前的日期,dateWithTimeIntervalSinceNow:
NSDate *yesterday = [NSDate dateWithTimeIntervalSinceNow: -(24 * 60 * 60)];
NSLog(@"yesterday:%@",yesterday);
NSDateFormatter *formatter =[[[NSDateFormatter alloc] init] autorelease];
NSDate *date = [NSDate date];
[formatter setTimeStyle:NSDateFormatterMediumStyle];
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comps = [[[NSDateComponents alloc] init] autorelease];
NSInteger unitFlags = NSYearCalendarUnit |
NSMonthCalendarUnit |
NSDayCalendarUnit |
NSWeekdayCalendarUnit |
NSHourCalendarUnit |
NSMinuteCalendarUnit |
NSSecondCalendarUnit;
//int week=0;
comps = [calendar components:unitFlags fromDate:date];
int week = [comps weekday];
int year=[comps year];
int month = [comps month];
int day = [comps day];
//[formatter setDateStyle:NSDateFormatterMediumStyle];
//This sets the label with the updated time.
int hour = [comps hour];
int min = [comps minute];
int sec = [comps second];
NSLog(@"week%d",week);
NSLog(@"year%d",year);
NSLog(@"month%d",month);
NSLog(@"day%d",day);
NSLog(@"hour%d",hour);
NSLog(@"min%d",min);
NSLog(@"sec%d",sec);
//得到毫秒
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterShortStyle];
//[dateFormatter setDateFormat:@"hh:mm:ss"]
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
NSLog(@"Date%@", [dateFormatter stringFromDate:[NSDate date]]);
[dateFormatter release];