NSDate类方法整理

来源:互联网 发布:部落冲突巨人数据 编辑:程序博客网 时间:2024/06/05 07:11

//****************格林尼治时间数据***********************

NSDate *date = [NSDate date];

NSLog(@"date=%@",date);


//****************将date格式转换成nsstring格式********************

NSDateFormatter *sd = [[NSDateFormatter alloc]init];


//设置date显示格式    

[sd setDateFormat:@"yyyy-MM-dd”];

//调用方法转换date为string

_tf.text = [sd stringFromDate:sender.date];


//date获取到的是格林威治时间

NSLog(@"%@",[sd stringFromDate:sender.date]);


//****************计算程序执行时间***********************

NSTimeInterval time1 = [date timeIntervalSinceNow];//程序执行时间减去当前时间

NSLog(@"time1 = %lf",time1);

NSTimeInterval time2 = [date timeIntervalSince1970];//程序执行时间减去1970年

NSLog(@"time2= %lf",time2);


//**************获取当前时间加上传递时间(秒)后的时间**************

date = [date dateByAddingTimeInterval:100];

NSLog(@"date=%@",date);


//**************获取遥远的将来时间**************

NSData *date1 = [NSDate distantFuture];

NSLog(@"date1=%@",date1);


//**************获取遥远的过去时间**************

NSData *date2 = [NSDate distantPast];

NSLog(@"date2=%@",date2);


//**************传入两个时间比较输出更早的时间**************

date = [date earlierDate:[NSDate date]];

NSLog(@"date=%@",date);


0 0