从属关系:NSDate:NSObject :Foundation
日期/时间在开发中经常使用, 但涉及到的无非是时间转字符串显示出来, 或者根据字符串获取时间对象, 其他的涉及很少.
下面介绍一些时间相关的函数
NSDate
一个NSDate对象, 代表一个具体的时间点, 这个时间点不是绝对的, 是相对的; 依赖于当前的时区, 会随着时区的变化而变化; NSDate默认相对的时区是GMT, 即: 格林威治标准时间;
获取日期实例的类方法主要有:
// 获取当前时间
+ (instancetype)date;
// 获取距离当前时间的为secs秒的时间
// secs 为正则为未来的一个时间; 为负则为过去的一个时间; 单位: 秒
+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;
// 距离参考时间间隔ti秒的一个时间点
// 这个参考时间默认是 2001-01-01 00:00:00
+ (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;
// 距离1970-01-01 00:00:00间隔secs秒的时间
+ (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
// 距离指定时间间隔secsToBeAdded秒的时间
+ (instancetype)dateWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
获取日期实例的实例方法:
- (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;
- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs;
- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
(1)获取系统当前时间
(2)用于格式化NSDate对象
(3)设置格式:yyyy-MM-dd HH:mm:ss zzz
(4)NSDate转NSString
// 获取当前时间 的标准格式 yyyy-MM-dd HH:mm:ss zzz
//(1)获取系统当前时间
NSDate *currentDate = [NSDate date];
//(2)用于格式化NSDate对象
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
//(3)设置格式:yyyy-MM-dd HH:mm:ss zzz
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss zzz"];
//(4)NSDate转NSString
NSString *currentDateString = [dateFormatter stringFromDate:currentDate];
NSLog(@"当前时间 = %@", currentDateString);
打印结果
2019-05-23 17:10:15.430735+0800 MGNSDate[2825:597235] 当前时间 = 2019-05-23 17:10:15 GMT+8
G: 公元时代,例如AD公元
yy: 年的后2位
yyyy: 完整年
MM: 月,显示为1-12,带前置0
MMM: 月,显示为英文月份简写,如 Jan
MMMM: 月,显示为英文月份全称,如 Janualy
dd: 日,2位数表示,如02
d: 日,1-2位显示,如2,无前置0
EEE: 简写星期几,如Sun
EEEE: 全写星期几,如Sunday
aa: 上下午,AM/PM
H: 时,24小时制,0-23
HH: 时,24小时制,带前置0
h: 时,12小时制,无前置0
hh: 时,12小时制,带前置0
m: 分,1-2位
mm: 分,2位,带前置0
s: 秒,1-2位
ss: 秒,2位,带前置0
S: 毫秒
Z: GMT(时区)
NSTimeZone 时区是和时间相关的最重要的一个因素, 因为各个地区的时区都不一样, 所以时间的差别是很大的, 这就是所谓的时差. 任何时区都以GMT为准:
GMT 0:00 格林威治标准时间
UTC +00:00 校准的全球时间
CCD +08:00 中国标准时间
而任何NSTimeZone对象所代表的时区都是相对于GMT的, iOS中的时间类NSDate所获取到的时间, 都是相对于GMT的.
iOS 中的时区表示方法:GMT+0800 GMT-0800。(+:东区 -:西区 08:小时数 00:分钟数)。
GMT+0830 就是指比GMT早8小时外加30分钟的时区
中国相关时区
Asia/Hong_Kong
Asia/Shanghai
Asia/Harbin
缩写
示例
//NSTimeZone
//1.获取NSTimeZone 实例对象:
NSTimeZone *timeZone1 = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
NSLog(@"timeZone1 = %@", timeZone1);
//其中28800就是相对GMT的的偏移量, 单位秒, 即8个小时(86060 = 28800)
//缩写, 其缩写可通过abbreviationDictionary得知
NSTimeZone *timeZone2 = [NSTimeZone timeZoneWithAbbreviation:@"HKT"];
NSLog(@"timeZone2 = %@", timeZone2);
//使用GMT+0800 的缩写获取
NSTimeZone *timeZone3 = [NSTimeZone timeZoneWithAbbreviation:@"GMT+0800"];
NSLog(@"timeZone3 = %@", timeZone3);
//通过相对于GMT的时间偏移量(时差)来获取时区, 注意这里的单位是秒:
NSTimeZone *timeZone4 = [NSTimeZone timeZoneForSecondsFromGMT:8*60*60];
NSLog(@"timeZone4 = %@", timeZone4);
执行结果:
2019-05-23 16:26:24.927730+0800 MGNSDate[2778:590099] timeZone1 = Asia/Shanghai (GMT+8) offset 28800
2019-05-23 16:26:24.927925+0800 MGNSDate[2778:590099] timeZone2 = Asia/Hong_Kong (GMT+8) offset 28800
2019-05-23 16:26:24.928291+0800 MGNSDate[2778:590099] timeZone3 = GMT+0800 (GMT+8) offset 28800
2019-05-23 16:26:24.928402+0800 MGNSDate[2778:590099] timeZone4 = GMT+0800 (GMT+8) offset 28800
时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数。通俗的讲, 时间戳是一份能够表示一份数据在一个特定时间点已经存在的完整的可验证的数据。 它的提出主要是为用户提供一份电子证据, 以证明用户的某些数据的产生时间。
获取当前时间戳
//获取当前时间戳
NSDate *currentDate2 = [NSDate date];
NSTimeInterval currentTime = [currentDate2 timeIntervalSince1970] * 1000;
NSString *timestampString1 = [NSString stringWithFormat:@"%f", currentTime];
NSLog(@"timestampString1 = %@",timestampString1);
执行结果
2019-05-23 17:33:46.909504+0800 MGNSDate[2848:601080] timestampString1 = 1558604026909.469971
时间戳转化成时间字符串
NSString *timestampString2 = @"1558603742596";
NSTimeInterval interval2 = [timestampString2 doubleValue] / 1000.0;
NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:interval2];
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
[formatter1 setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
NSString *dateString = [formatter1 stringFromDate: date2];
NSLog(@"dateString = %@",dateString);
执行结果
2019-05-23 17:33:46.909675+0800 MGNSDate[2848:601080] dateString = 2019-05-23 17:29:02
//通过时间戳计算当前星期
NSString *weekday = @"";
NSString *timestampString3 = @"1558603742596";
NSTimeInterval interval3 = [timestampString3 doubleValue] / 1000.0;
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSInteger unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |NSCalendarUnitHour | NSCalendarUnitWeekday |
NSCalendarUnitHour |NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDate *date3 = [NSDate dateWithTimeIntervalSince1970:interval3];
NSDateComponents *comps = [calendar components:unitFlags fromDate:date3];
switch ([comps weekday]) {
case 1:
weekday = @"星期日";
break;
case 2:
weekday = @"星期一";
break;
case 3:
weekday = @"星期二";
break;
case 4:
weekday = @"星期三";
break;
case 5:
weekday = @"星期四";
break;
case 6:
weekday = @"星期五";
break;
case 7:
weekday = @"星期六";
break;
}
NSLog(@"weekday = %@",weekday);
打印结果:
2019-05-23 17:41:29.516920+0800 MGNSDate[2851:602032] weekday = 星期四
- (NSComparisonResult)compare:(NSDate *)other;
结构体
typedef NS_ENUM(NSInteger, NSComparisonResult) {
NSOrderedAscending = -1, // 早
NSOrderedSame, // 相等
NSOrderedDescending // 晚
};
//两个时间比较
//- (NSComparisonResult)compare:(NSDate *)other;
/*
typedef NS_ENUM(NSInteger, NSComparisonResult) {
NSOrderedAscending = -1L, // 早
NSOrderedSame, // 相等
NSOrderedDescending // 晚
};
*/
NSDate *currentDate3 = [NSDate date];
NSTimeInterval hour = 60 * 60;
// 一小时后
NSDate *date4 = [currentDate3 dateByAddingTimeInterval:hour];
NSLog(@"date4 = %@",date4);
// 一小时前
NSDate *date5 = [currentDate3 dateByAddingTimeInterval:-hour];
NSLog(@"date = %@",date5);
NSDate *currentDate4 = [NSDate date];
NSTimeInterval day = 24 * 60 * 60;
// 昨天
NSDate *yesterday = [currentDate4 dateByAddingTimeInterval:-day];
NSLog(@"yesterday = %@",yesterday);
// 今天
NSDate *today = [currentDate4 dateByAddingTimeInterval:0];
NSLog(@"today = %@",today);
// 明天
NSDate *tomorrow = [currentDate4 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(@"两个日期降序");
}
NSDate *date6 = [NSDate date];
NSDate *date7 = [NSDate date];
// 返回比较早的那个时间
NSDate *date8 = [date6 earlierDate:date7];
NSLog(@"date8 = %@",date8);
// 返回比较晚的那个时间
NSDate *date9 = [date6 laterDate:date7];
NSLog(@"date9 = %@",date9);
执行结果:
2019-05-24 08:45:25.478179+0800 MGNSDate[3522:742971] date4 = Fri May 24 09:45:25 2019
2019-05-24 08:45:25.478234+0800 MGNSDate[3522:742971] date = Fri May 24 07:45:25 2019
2019-05-24 08:45:25.478285+0800 MGNSDate[3522:742971] yesterday = Thu May 23 08:45:25 2019
2019-05-24 08:45:25.478316+0800 MGNSDate[3522:742971] today = Fri May 24 08:45:25 2019
2019-05-24 08:45:25.478331+0800 MGNSDate[3522:742971] tomorrow = Sat May 25 08:45:25 2019
2019-05-24 08:45:25.478378+0800 MGNSDate[3522:742971] 两个日期不相同
2019-05-24 08:45:25.478435+0800 MGNSDate[3522:742971] 日期升序
2019-05-24 08:45:25.478538+0800 MGNSDate[3522:742971] date8 = Fri May 24 08:45:25 2019
2019-05-24 08:45:25.478554+0800 MGNSDate[3522:742971] date9 = Fri May 24 08:45:25 2019
//其他
//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);
// 返回1970-1-1开始走过的毫秒数
NSDate *date10 = [NSDate date];
NSTimeInterval interval4 = [date10 timeIntervalSince1970];
NSLog(@"interval4 = %f",interval4);
执行结果:
2019-05-24 08:50:10.917017+0800 MGNSDate[3527:744312] future = Mon Jan 1 08:00:00 4001
2019-05-24 08:50:10.917042+0800 MGNSDate[3527:744312] past = Sat Dec 30 08:05:43 0000
2019-05-24 08:50:10.917054+0800 MGNSDate[3527:744312] interval4 = 1558659010.917051
/* NSDate.h
Copyright (c) 1994-2018, Apple Inc. All rights reserved.
*/
#import
#import
@class NSString;
NS_ASSUME_NONNULL_BEGIN
FOUNDATION_EXPORT NSNotificationName const NSSystemClockDidChangeNotification API_AVAILABLE(macos(10.6), ios(4.0), watchos(2.0), tvos(9.0));
typedef double NSTimeInterval;
#define NSTimeIntervalSince1970 978307200.0
@interface NSDate : NSObject
@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
@interface NSDate (NSExtendedDate)
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;
@property (readonly) NSTimeInterval timeIntervalSinceNow;
@property (readonly) NSTimeInterval timeIntervalSince1970;
- (id)addTimeInterval:(NSTimeInterval)seconds API_DEPRECATED("Use dateByAddingTimeInterval instead", macos(10.0,10.6), ios(2.0,4.0), watchos(2.0,2.0), tvos(9.0,9.0));
- (instancetype)dateByAddingTimeInterval:(NSTimeInterval)ti API_AVAILABLE(macos(10.6), ios(2.0), watchos(2.0), tvos(9.0));
- (NSDate *)earlierDate:(NSDate *)anotherDate;
- (NSDate *)laterDate:(NSDate *)anotherDate;
- (NSComparisonResult)compare:(NSDate *)other;
- (BOOL)isEqualToDate:(NSDate *)otherDate;
@property (readonly, copy) NSString *description;
- (NSString *)descriptionWithLocale:(nullable id)locale;
@property (class, readonly) NSTimeInterval timeIntervalSinceReferenceDate;
@end
@interface NSDate (NSDateCreation)
+ (instancetype)date;
+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;
+ (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;
+ (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
+ (instancetype)dateWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
@property (class, readonly, copy) NSDate *distantFuture;
@property (class, readonly, copy) NSDate *distantPast;
- (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;
- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs;
- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
@end
NS_ASSUME_NONNULL_END