NSDate *date=[
NSDate
date];
//
获取当前时间
NSLog( @"%@",date); //2015-11-03 03:15:10 +0000
NSDate *date1=[ NSDate dateWithTimeIntervalSince1970: 1* 30* 24* 3600]; // 从 1970 经过若干秒后的时间
NSLog( @"%@",date1); //1970-01-31 00:00:00 +0000
NSDate *date2=[ NSDate dateWithTimeIntervalSinceNow: 1* 12* 30* 24* 3600]; // 当前时间经过若干秒后的时间
NSLog( @"%@",date2); //2016-10-28 03:20:57 +0000
NSDate *date3=[date1 dateByAddingTimeInterval: 60]; // 以某时间为准,加上若干秒后的时间
NSLog( @"%@",date3); //1970-01-31 00:01:00 +0000
NSLog( @"%@",date); //2015-11-03 03:15:10 +0000
NSDate *date1=[ NSDate dateWithTimeIntervalSince1970: 1* 30* 24* 3600]; // 从 1970 经过若干秒后的时间
NSLog( @"%@",date1); //1970-01-31 00:00:00 +0000
NSDate *date2=[ NSDate dateWithTimeIntervalSinceNow: 1* 12* 30* 24* 3600]; // 当前时间经过若干秒后的时间
NSLog( @"%@",date2); //2016-10-28 03:20:57 +0000
NSDate *date3=[date1 dateByAddingTimeInterval: 60]; // 以某时间为准,加上若干秒后的时间
NSLog( @"%@",date3); //1970-01-31 00:01:00 +0000
NSTimeInterval interval11=[date1
timeIntervalSinceNow];
//date1-
当前时间=的时间
NSTimeInterval interval =[date
timeIntervalSince1970];
//
从
1970
年到
date
时间的秒数
NSTimeInterval interval1=[date1 timeIntervalSinceDate:date3]; //date1-date3 的时间
NSLog( @"%lf",interval1); //-60.000000
NSDate *later=[date laterDate:date1]; 获取两个时间较晚的(距离 1970 年时间较长的)
NSDate *earl=[date earlierDate:date1]; 获取两个时间较早的
NSLog( @"%@",earl); //1970-01-31 00:00:00 +0000
NSTimeInterval interval1=[date1 timeIntervalSinceDate:date3]; //date1-date3 的时间
NSLog( @"%lf",interval1); //-60.000000
NSDate *later=[date laterDate:date1]; 获取两个时间较晚的(距离 1970 年时间较长的)
NSDate *earl=[date earlierDate:date1]; 获取两个时间较早的
NSLog( @"%@",earl); //1970-01-31 00:00:00 +0000
//
格式化日期对象(把日期对象转换成字符串)
NSDateFormatter *dateformatter=[ NSDateFormatter new];
[dateformatter setDateFormat: @"yyyy 年 MM 月 dd 日 HH:mm:ss a"]; //
NSString *localDateTime=[dateformatter stringFromDate:date];
NSLog( @"%@",localDateTime); //2015 年 11 月 03 日 14:53:54 下午
// 把日期格式的字符串转换成日期对象
NSString *myDateTime= @"2015/01/10 11:35:00";
NSDateFormatter *formatter=[ NSDateFormatter new];
[formatter setDateFormat: @"yyyy/MM/dd HH:mm:ss"];
NSDate *newDate=[formatter dateFromString:myDateTime];
NSDateFormatter *dateformatter=[ NSDateFormatter new];
[dateformatter setDateFormat: @"yyyy 年 MM 月 dd 日 HH:mm:ss a"]; //
NSString *localDateTime=[dateformatter stringFromDate:date];
NSLog( @"%@",localDateTime); //2015 年 11 月 03 日 14:53:54 下午
// 把日期格式的字符串转换成日期对象
NSString *myDateTime= @"2015/01/10 11:35:00";
NSDateFormatter *formatter=[ NSDateFormatter new];
[formatter setDateFormat: @"yyyy/MM/dd HH:mm:ss"];
NSDate *newDate=[formatter dateFromString:myDateTime];
NSLog(
@"%@",newDate);
//2015-01-10 03:35:00 +0000
/*
字符转换存在时区差,三种补偿方法
*/
//
NSTimeZone *zone=[ NSTimeZone systemTimeZone]; // 获得系统时区
NSLog( @"%@",zone); //Asia/Shanghai (GMT+8) offset 28800
NSInteger timeSub=[zone secondsFromGMT]; // 和格林尼治时间差
newDate=[newDate dateByAddingTimeInterval:timeSub]; // 补上时差
NSLog( @"%@",newDate); //2015-01-10 11:35:00 +0000
//
[formatter setTimeZone:[ NSTimeZone timeZoneWithName: @"UTC"]]; // 将时区设置为国际校准时间
NSDate *newDate1=[formatter dateFromString:myDateTime]; // 字符串转换时间
NSLog( @"%@",newDate1); //2015-01-10 11:35:00 +0000
//
[formatter setTimeZone:[ NSTimeZone timeZoneForSecondsFromGMT: 0]]; // 意思和第二种 @“UTC” 一样
//
NSTimeZone *zone=[ NSTimeZone systemTimeZone]; // 获得系统时区
NSLog( @"%@",zone); //Asia/Shanghai (GMT+8) offset 28800
NSInteger timeSub=[zone secondsFromGMT]; // 和格林尼治时间差
newDate=[newDate dateByAddingTimeInterval:timeSub]; // 补上时差
NSLog( @"%@",newDate); //2015-01-10 11:35:00 +0000
//
[formatter setTimeZone:[ NSTimeZone timeZoneWithName: @"UTC"]]; // 将时区设置为国际校准时间
NSDate *newDate1=[formatter dateFromString:myDateTime]; // 字符串转换时间
NSLog( @"%@",newDate1); //2015-01-10 11:35:00 +0000
//
[formatter setTimeZone:[ NSTimeZone timeZoneForSecondsFromGMT: 0]]; // 意思和第二种 @“UTC” 一样
@interface NSDate (NSDateCreation)
+ (
instancetype)date;
//
获取当前时间
+ (
instancetype)dateWithTimeIntervalSinceNow:(
NSTimeInterval)secs;
//
返回以当前时间为基准,然后过了
secs
秒的时间
+ (
instancetype)dateWithTimeIntervalSinceReferenceDate:(
NSTimeInterval)ti;
+ (
instancetype)dateWithTimeIntervalSince1970:(
NSTimeInterval)secs;
//
返回以
1970
年时间为基准,然后过了
secs
秒的时间
- ( instancetype)initWithTimeIntervalSinceNow:( NSTimeInterval)secs;
- ( instancetype)initWithTimeIntervalSince1970:( NSTimeInterval)secs;
- ( instancetype)initWithTimeInterval:( NSTimeInterval)secsToBeAdded sinceDate:( NSDate *)date;
@end
@interface NSDate (NSExtendedDate)
- (
NSTimeInterval)timeIntervalSinceDate:(
NSDate *)anotherDate;
//
计算两个时间之间的间隔(秒数)
- (
NSTimeInterval)timeIntervalSinceNow;
//
返回给定时间与当前时间的时间差(秒数)
- (
NSTimeInterval)timeIntervalSince1970;
//
获取当前时间(秒数)
//
表示从
1970
年到给定时间的秒数
- (
id)dateByAddingTimeInterval:(
NSTimeInterval)ti;
//
以某时间为准,加上若干秒后的时间
- (
NSDate *)earlierDate:(
NSDate *)anotherDate;
//
获取两个日期时间对象中较早的一个
- (
NSDate *)laterDate:(
NSDate *)anotherDate;
//
获取两个日期时间对象中较晚的一个
- (
NSComparisonResult)compare:(
NSDate *)other;
- (
BOOL)isEqualToDate:(
NSDate *)otherDate;
+ ( NSTimeInterval)timeIntervalSinceReferenceDate;
@end
- (
NSString *)stringFromDate:(
NSDate *)date;
//
格式化日期时间对象
(
把日期对象转换成字符串
)
- (
NSDate *)dateFromString:(
NSString *)string;
//
把日期格式的字符串转换成日期对象
- (
void)setTimeZone:(
NSTimeZone *)tz;
//系统时区
- (
void)setDateFormat:(
NSString *)string;
//
设置日期显示格式
版权声明:本文为博主原创文章,未经博主允许不得转载。