/*
NSDate 处理时间的类
默认打印的是 0 时区的时间
*/
/*
// 实例化一个 NSDate 对象 (使用+date)
NSDate *date = [NSDate date];
NSLog(@"%@",date);//默认是0时区的时间
// 获取东八区的当前时间 相差八小时 8*60*60
NSDate *nowDate = [NSDate dateWithTimeIntervalSinceNow:8*60*60];
NSLog(@"%@",nowDate);
// 计算的是在1970-01-01 00-00-00 的基础上加了一个小时
NSDate *date1 = [NSDate dateWithTimeIntervalSince1970:1*60*60];
NSLog(@"%@",date1);
// 计算的是在1970-01-01 00-00-00 的基础上了减一个小时
NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:-1*60*60];
NSLog(@"%@",date2);
// 计算的是在2001-01-01 00-00-00 的基础上加了一个小时
NSDate *date3 = [NSDate dateWithTimeIntervalSinceReferenceDate:1*60*60];
NSLog(@"%@",date3);
// 自定义初始时间
NSDate *date4 = [NSDate dateWithTimeInterval:8*60*60 sinceDate:[NSDate date]];
NSLog(@"%@",date4);
NSDate *yesterday = [NSDate dateWithTimeInterval:-24*60*60 sinceDate:date4];
NSLog(@"%@",yesterday);
NSDate *tomorrow = [NSDate dateWithTimeInterval:24*60*60 sinceDate:[NSDate dateWithTimeInterval:8*60*60 sinceDate:[NSDate date]]];
NSLog(@"%@",tomorrow);
// 计算两个人日期相差的时间
NSTimeInterval yest = [yesterday timeIntervalSinceDate:tomorrow];
NSLog(@"%.2lf",yest/3600);
NSTimeInterval time1 = [tomorrow timeIntervalSinceNow];
NSLog(@"%.2lf",time1/3600);
// ------>>>>>>>>>>>>>>>>>>> NSDateFormatter 可以设定转化格式,完成 NSString 和 NSDate的转换
// date转换为字符串
// 系统给定格式
NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
// 设置日期格式
[formatter1 setDateStyle:NSDateFormatterFullStyle];
NSLog(@"%@",[formatter1 stringFromDate:[NSDate date]]);
// 自定义的格式
NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
[formatter2 setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
NSLog(@"%@",[formatter2 stringFromDate:[NSDate date]]);
// 字符串 转换为 date
NSDate *d1 = [formatter2 dateFromString:@"2000/02/01 12:12:12"];
NSLog(@"%@",d1);//打印d1的时候默认打印的是0时区的时间
*/
oc NSDate
最新推荐文章于 2016-03-17 14:43:55 发布