* NSDate 日期
* NSTimeInterval 时间间隔
* NSDateFormatter 时间格式器
时间的推算
* NSDate 日期相关的类 GTM(国际标准时间)
时间戳:从1970年到现在 的时间间隔(字符串)可以表示一个唯一的时间标识
三种当前时间的初始化:
//1、当前时间
NSDate *date = [NSDatedate];
NSLog(@"%@",date);
// 2、 (NSTimeInterval)是一个时间间隔单位是秒
//从现在开始过了多少秒过去的时间是负数 未来的时间是正数
NSTimeInterval interval = 60*60*8;
NSDate *date1 = [NSDatedateWithTimeIntervalSinceNow:interval];
NSLog(@"%@",date1);
//3、获得时间戳所表示的日期
NSDate *date3 = [NSDatedateWithTimeIntervalSince1970:1452044015];
NSLog(@"date1:%@",date3);
日期怎么转换成时间间隔
1、可以获得时间间隔(1970-现在)
2、可以获得两个日期之间的时间间隔
// - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;
//1452044015这个时间戳对应的日期与当前时间的 时间间隔
NSTimeInterval timeInterVal = fabs([date3 timeIntervalSinceDate:[NSDatedate]]);
NSLog(@"%f",timeInterVal);
//计算两个日期 差多少小时多少分 多少秒
int h = timeInterVal/(60*60);
int remainTimeInterVal =((int)timeInterVal)%(60*60);
NSLog(@"%d",h);
int m = remainTimeInterVal/60;
NSLog(@"%d",m);
int s =remainTimeInterVal%60;
NSLog(@"%d",s);
***日期 转时间戳
NSDate *curDate = [NSDatedate];
NSLog(@"%f",curDate.timeIntervalSince1970);
NSString *timeStamp = [NSStringstringWithFormat:@"%d",(int)curDate.timeIntervalSince1970];
NSLog(@"%@",timeStamp);
#pragma mark***********日期时间格式*********************
//格式器初始化
NSDateFormatter *formatter = [[NSDateFormatteralloc]init];
//格式的属性
formatter.dateFormat =@"yyyy-MM-dd HH:mm:ss";
//1、把日期转换成字符串
//(1)日期
NSDate *now = [NSDatedate];
//2、使用日期格式器进行转换
NSString *dateString = [formatter stringFromDate:now];
NSLog(@"%@",dateString);
//3、把字符串转换成日期
NSDate *date10 = [formatter dateFromString:dateString];
NSLog(@"%@",date10);
//1451847216
//1、找到时间戳所在日期
NSDate *newDate = [NSDatedateWithTimeIntervalSince1970:(1451847216)];
//2、初始化日期格式器 并指定格式
NSDateFormatter *ddateFormatter = [[NSDateFormatteralloc]init];
// 某年某月某日某周几 上下午
ddateFormatter.dateFormat = @"yyyy年MM月dd日 E a";
//3、使用格式器开始转换
NSString *newString = [ddateFormatter stringFromDate:newDate];
NSLog(@"%@",newString);
日期格式如下:
y 年
M 年中的月份
D 当天是今年的第多少天
d 月份中的天数
F 月份中的周数
E 星期几
a Am/pm
H 一天中的小时数(0-23)
k 一天中的小时数(1-24)
K am/pm 中的小时数(0-11) Number 0
h am/pm 中的小时数(1-12) Number 12
m 小时中的分钟数 Number 30
s 分钟中的秒数 Number 55
S 毫秒数 Number 978
z 时区 General time zone Pacific Standard Time; PST; GMT-08:00
Z 时区 RFC 822 time zone -0800