NSObject--NSDate常用方法




        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
       



        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
       
        


        // 格式化日期对象(把日期对象转换成字符串)
        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” 一样

       

 

 
@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; // 设置日期显示格式




版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/JinigW/p/4934961.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值