1、什么是UTC?
世界标准时间,国际协调时间,简称UTC。不属于任意时区。
2、时间戳?
就是1970.1.1 00:00:00 作为标准,某个时间和它的秒数,并且NSDate必须是0时区的,UTC格式的。
3、时间戳应该是10位,如果不巧碰到了13位的,代表着它计算了毫秒,只要删除剪切前10位就行了。
// 获取的是0时区的时间,显示的是格林尼治是时间:年-月-日 时:分:秒:+ 时区。
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
[dateFormatter setDateFormat:@"yyyy年MM年dd日 HH时mm分ss秒"];
// 设置为系统时区
dateFormater.timeZone = [NSTimeZone resetSystemTimeZone];
// 获取的是当前时区的时间。
NSString *dayStr = [dateFormatter stringFromDate:date];
NSDate -> NSString,而且转成字符串的文字格式多样,主要依赖DateFormat,但是可以随意确定DateFormat格式,都能输出。(但是NSString -> NSDate不可以随便的转换,必须要看NSString是什么格式的,然后再去写dateFormat,否则无效)
创建时间
// 获取的是东8区此刻的时间
NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:8 * 60 * 60];
// 获取昨天此刻的时间
NSDate *yesterdayDate = [NSDate dateWithTimeIntervalSinceNow:(-24 + 8) * 60 * 60];
// 获取明天此刻的时间
NSDate *tomorrowDate = [NSDate dateWithTimeInterval:24 * 60 * 60 sinceDate:date1];
NSDate *tomorrowDate1 = [NSDate dateWithTimeIntervalSinceNow:24 * 60 * 60];
// 获取时间间隔
NSTimeInterval timeInterval = [tomorrowDate timeIntervalSinceDate:yesterdayDate];
NSComparisonResult result = [tomorrowDate compare:tomorrowDate];
NSDateFormatter
// 获取所有时区的名字存入数组
NSArray *worldTimeZone = [NSTimeZone knownTimeZoneNames];
// 指定时区名字获取指定城市的当地时间
dateFormatter.timeZone =[NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
// 指定输出的格式 这里格式必须是和上面定义字符串的格式相同,否则输出空
[dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
NSString *dateStr = [dateFormatter stringFromDate:date];
NSDateFormatter 的一些格式介绍
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; //这里要注意的是formatter的格式,如果是小写的"hh",那么时间将会跟着系统设置变成12小时或者24小时制。大写的"HH",则强制为24小时制。
[dateFormatter setDateFormat:@"yyyy年MM月dd日#EEEE"];EEEE为星期几,EEE为周几
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setDateFormat:@"yyyy年MMMMd日"];//MMMM 为xx月,一个d可以省去01日前的0
NSDateFormatter 格式化参数如下:
G: 公元时代,例如AD公元
yy: 年的后2位
yyyy: 完整年
MM: 月,显示为1-12
MMM: 月,显示为英文月份简写,如 Jan
MMMM: 月,显示为英文月份全称,如 Janualy
dd: 日,2位数表示,如02
d: 日,1-2位显示,如 2
EEE: 简写星期几,如Sun
EEEE: 全写星期几,如Sunday
aa: 上下午,AM/PM
H: 时,24小时制,0-23
K:时,12小时制,0-11
m: 分,1-2位
mm: 分,2位
s: 秒,1-2位
ss: 秒,2位
S: 毫秒
常用日期结构:
yyyy-MM-dd HH:mm:ss.SSS
yyyy-MM-dd HH:mm:ss
yyyy-MM-dd
MM dd yyyy
字符串和时间的转换
NSString *dateString = [NSString stringWithFormat:@"2017-5-25 12:12:12"];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"date--->%@",date);
NSString *dateStr = [dateFormatter stringFromDate:date];
NSLog(@"dateStr--->%@",dateStr);
创建时间 NSDate
// 以2001年1月1日 0:0:0 的偏移秒数来初始化,将打印:2001-01-01 00:01:20 +0000
- (instancetype)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;
+ (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;
// 以当前时间偏移秒数来初始化
- (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;
+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;
// 以1970 的时间偏移秒数来初始化
- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs;
+ (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
// 以当前NSDate对象为基准时间来进行秒数偏移
- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
+ (instancetype)dateWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
- (instancetype)dateByAddingTimeInterval:(NSTimeInterval)ti
// 返回两时间相差的秒数
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;
@property (readonly) NSTimeInterval timeIntervalSinceNow;
// 从1970-1-1 0:0:0开始,经过多少秒到达对象指定时间。
@property (readonly) NSTimeInterval timeIntervalSince1970; // 时间戳
// 从2001-1-1 0:0:0开始,经过多少秒到达对象指定时间。
@property (readonly) NSTimeInterval timeIntervalSinceReferenceDate;
// 时间比较
- (NSDate *)earlierDate:(NSDate *)anotherDate;
- (NSDate *)laterDate:(NSDate *)anotherDate;
- (NSComparisonResult)compare:(NSDate *)other;
- (BOOL)isEqualToDate:(NSDate *)otherDate;
+ (id)distantPast 与 + (id)distantFuture
这两个是类方法,分别用来返回一个极早的时间点和一个极晚的时间点
NSDate *date = [NSDate distantFuture];
NSLog(@"future date is %@",date);
NSDate *date2 = [NSDate distantPast];
NSLog(@"past date is %@",date2);
distantPast将返回:0001-12-30 00:00:00 +0000,distantFuture将返回:4001-01-01 00:00:00 +0000
NSTimeZone
1. + (id)timeZoneWithName:(NSString *)aTimeZoneName / - (id)initWithName:(NSString *)aName
根据时区名称初始化。可以调用NSTimeZone的类方法 + (NSArray *)knownTimeZoneNames来返回所有已知的时区名称。
@property (class, readonly, copy) NSArray<NSString *> *knownTimeZoneNames;
NSArray *zoneArray = [NSTimeZone knownTimeZoneNames];
for(NSString *str in zoneArray) {
NSLog(@"%@",str);
}
2.+ (nullable instancetype)timeZoneWithAbbreviation:(NSString *)abbreviation;
根据时区缩写来初始化。
3.+ (NSTimeZone *)systemTimeZone
返回系统时区
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSLog(@"%@",zone);
4. + (NSTimeZone *)localTimeZone
返回本地时区,与systemTimeZone的区别在于:本地时区可以被修改,而系统时区不能修改。
[NSTimeZone setDefaultTimeZone:[[NSTimeZone alloc] initWithName:@"America/Chicago"]];
NSTimeZone *systemZone = [NSTimeZone systemTimeZone];
NSTimeZone *localZone = [NSTimeZone localTimeZone];
NSLog(@"%@",systemZone);
NSLog(@"%@",localZone);
网友评论