- NSDate * date =[NSDate date];
注意:date方法返回的是当前时间(now), 但是这个时间是格林尼治时间(GMT),并不是我们传统意义上的北京时间. 格林威治时间属于0时区,北京属于东8区;
比如: 北京时间是上午11:04
打印出来的结果确是
2>- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date
示例
- NSDate * date = [[NSDate alloc] initWithTimeInterval:60 sinceDate:[NSDate date]];
含义: 获取从某个日期开始往前或者往后多久的日期,此处60代表60秒,如果需要获取之前的,将60改为-60即可
注意:NSTimeInterval 是双精度类型,单位是秒.
typedefdouble NSTimeInterval;
三,其它相关方法和属性
typedef double NSTimeInterval;
#define NSTimeIntervalSince1970 978307200.0
1>NSDate类
- @interface NSDate : NSObject <NSCopying, NSSecureCoding>
- @property (readonly) NSTimeInterval timeIntervalSinceReferenceDate;
- - (instancetype)init NS_DESIGNATED_INITIALIZER;
- - (instancetype)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti NS_DESIGNATED_INITIALIZER;
- - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
- @end
2>NSDate分类拓展方法_NSDate (NSExtendedDate)
- @interface NSDate (NSExtendedDate)
- - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;
- @property (readonly) NSTimeInterval timeIntervalSinceNow;
- @property (readonly) NSTimeInterval timeIntervalSince1970;
- - (id)addTimeInterval:(NSTimeInterval)secs;//返回以目前的实例中保存的时间为基准,然后过了secs秒的时间
- - (instancetype)dateByAddingTimeInterval:(NSTimeInterval)ti;
-
- - (NSDate *)earlierDate:(NSDate *)anotherDate;
- - (NSDate *)laterDate:(NSDate *)anotherDate;
- - (NSComparisonResult)compare:(NSDate *)other;
- 该方法用于排序时调用:
. 当实例保存的日期值与anotherDate相同时返回NSOrderedSame
. 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending
. 当实例保存的日期值早于anotherDate时返回NSOrderedAscending - - (BOOL)isEqualToDate:(NSDate *)otherDate;//与otherDate比较,相同返回YES
-
- @property (readonly, copy) NSString *description;
- - (NSString *)descriptionWithLocale:(nullable id)locale;
- + (NSTimeInterval)timeIntervalSinceReferenceDate;
- @end
3>NSDate分类拓展方法_NSDate (NSDateCreation)
- @interface NSDate (NSDateCreation)
-
- + (instancetype)date;
- + (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;
- + (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;
- + (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
- + (instancetype)dateWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
-
- + (NSDate *)distantFuture;
- + (NSDate *)distantPast;
- 注意: iOS中用NSDate表示的时间只能在distantPast和distantFuture之间!
-
-
- - (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;
- - (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs;
- - (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
- @end
四,相关用法
1>显示当前时间
NSDate ->NSString
- NSDate *date = [NSDate date];
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"yyyy/MM/dd hh:mm:ss"];
- self.timeLabel.text = [dateFormatter stringFromDate:date];
NSString ->NSDate
- NSString *dateString = self.timeLabel.text;
- NSDate *nowDate = [dateFormatter dateFromString:dateString];
- self.timeLabel2.text = [dateFormatter stringFromDate:nowDate];
2>获取指定日期的年,月,日,时,分,秒
第一种写法:
- [dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
- NSString *dateString = @"2016/06/06 3:2:34";
- NSDate *theDate = [dateFormatter dateFromString:dateString];
- NSDateComponents *com = [[NSCalendar currentCalendar]components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:theDate]; NSInteger year = com.year;
- NSInteger month = com.month;
- NSInteger day = com.day;
- NSInteger hour = com.hour;
- NSInteger minute = com.minute;
- NSInteger second = com.second;
- self.timeLabel2.text = [NSString stringWithFormat:@"%ld年%ld月%ld日 %02ld时:%02ld分:%02ld秒, year, month, day, hour, minute, second];
第二种写法:
- NSString *dateString = @"2016/06/06 3:2:34";
- NSDate *theDate = [dateFormatter dateFromString:dateString];
- NSCalendar *calendar = [NSCalendar currentCalendar];
- NSUInteger unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond;
- NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:theDate];
- NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
-
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
- if ([cmp1 day] == [cmp2 day]) {
- formatter.dateFormat = @"今天 HH:mm";
- } else if ([cmp1 year] == [cmp2 year]) {
- formatter.dateFormat = @"MM-dd HH:mm";
- } else {
- formatter.dateFormat = @"yyyy-MM-dd HH:mm";
- }
- NSString *time = [formatter stringFromDate:theDate];
-
- self.timeLabel2.text = time;
注意:通过改变NSDateFormatter的dateFormat表现形式可以实现各种你想要的时间表示形式,
比如
- NSString *dateString = @"2015/09/08 3:2:34";
- NSDate *theDate = [dateFormatter dateFromString:dateString];
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
- formatter.dateFormat = @"'日期:'yyyy有鬼MM