OC 时间NSDate (NSDate<——> NSString)

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        //NSDate 对象的初始化
        NSDate *nDate=[NSDate dateWithString:@"2013-08-06 08:09:10 +0000"];
        NSDate *nDate2=[NSDate dateWithString:@"2013-01-01 00:00:00 +0000"];
        NSLog(@"%@",nDate);// 输出 2013-08-06 08:09:10 +0000
        
        // 以YYYY-MM-DD HH:MM:SS ±HHMM的格式表示时间。
        // 其中 "±HHMM" 表示与GMT的存在多少小时多少分钟的时区差异。比如,若时区设置在北京,则 "±HHMM" 显示为 "+0800"
        
        /**********将 NSDate 转换 为 NSString **********/
        NSString *Str=[nDate description ];//
        NSLog(@"%@",Str);
        
        /****************一些常用方法**********************************/
        // 方法:+ (id)date,返回当前时间
        NSDate*Date1=[NSDate date];
        NSLog(@"%@",Date1);//输出 XXXX-XX-XX XX:XX:XX +0000 格式
        
        //方法:+ (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs
        //返回以当前时间为基准,然后过了secs秒的时间
        NSDate *Date2=[NSDate dateWithTimeIntervalSinceNow:600];
        NSLog(@"%@",Date2);
        
        // + (id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
        //返回以1970/01/01 GMT为基准,然后过了secs秒的时间
        NSDate *Date3=[NSDate dateWithTimeIntervalSince1970:600];
        NSLog(@"%@",Date3);//1970-01-01 00:10:00 +0000
        
        //+ (id)distantFuture;
        //返回很多年以后的未来的某一天
        NSDate *Date4=[NSDate distantFuture];
        NSLog(@"%@",Date4);//4001-01-01 00:00:00 +0000
        
        //+ (id)distantPast;
        //返回很多年以前的某一天。
        NSDate *Date5=[NSDate distantPast];
        NSLog(@"Date5:%@",Date5);//0001-12-30 00:00:00 +0000
        
        //- (id)addTimeInterval:(NSTimeInterval)secs;
        //返回以目前的实例中保存的时间为基准,然后过了secs秒的时间
        NSDate *Date6=[nDate addTimeInterval:600];//有问题 请大神指点
        NSLog(@"Date6:%@",Date6);//Date6:2013-08-06 08:19:10 +0000
        
        // - (id)init;
        //初始化为当前时间。类似date方法
        NSDate *Date7=[Date1 init];//等于Date1
        NSLog(@"Date7:%@",Date7);
        
        //- (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;
        // 初始化为以2001/01/01 GMT为基准,然后过了secs秒的时间。类似dateWithTimeIntervalSinceReferenceDate:方法
        NSDate *Date8=[Date2 initWithTimeIntervalSinceReferenceDate:600];
        NSLog(@"%@",Date8);  // 2001-01-01 00:10:00 +0000
        
        //  - (id)initWithTimeInterval:(NSTimeInterval)secs sinceDate:(NSDate *)refDate;
        //初始化为以refDate为基准,然后过了secs秒的时间
        NSDate *Date9=[Date1 initWithTimeInterval:600 sinceDate:[NSDate dateWithString:@"2013-08-06 08:39:10 +0000"]];
        NSLog(@"%@",Date9);//2013-08-06 08:49:10 +0000
        
        /***********日期之间比较 方法*************/
        
        //- (BOOL)isEqualToDate:(NSDate *)otherDate;
        //与otherDate比较,相同返回YES
        if ([nDate isEqualToDate:nDate2]==NO) {
            NSLog(@"这两个时间 不相同");
        }
        else
        {
            NSLog(@"这两个时间 相同");
        }
        
        //- (NSDate *)earlierDate:(NSDate *)anotherDate;
        //与anotherDate比较,返回较早的那个日期
        NSLog(@"%@",[nDate earlierDate:nDate2]);//2013-01-01 00:00:00 +0000
        
        // - (NSDate *)laterDate:(NSDate *)anotherDate;
        //与anotherDate比较,返回较晚的那个日期
        NSLog(@"%@",[nDate laterDate:nDate2]);//2013-08-06 08:09:10 +0000
        
        /*
         
         - (NSComparisonResult)compare:(NSDate *)other;
         该方法用于排序时调用:
         . 当实例保存的日期值与anotherDate相同时返回NSOrderedSame
         . 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending
         . 当实例保存的日期值早于anotherDate时返回NSOrderedAscending
         
         */
        
         /*******取回时间间隔可用以下方法******/
        //定义两个 NSDate
        NSDate *TiDate1=[NSDate dateWithString:@"2013-08-06 08:00:10 +0000"];
        NSDate *TiDate2=[NSDate dateWithString:@"2013-08-06 08:10:10 +0000"];
        
        //- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate;
        //以refDate为基准时间,返回实例保存的时间与refDate的时间间隔
        NSLog(@"%f",[TiDate1 timeIntervalSinceDate:TiDate2]);//-600.000000
        
        //- (NSTimeInterval)timeIntervalSinceNow;
        //以当前时间(Now)为基准时间,返回实例保存的时间与当前时间(Now)的时间间隔
        NSLog(@"%f",[TiDate1 timeIntervalSinceNow]);//-1267744.063120
        
        // - (NSTimeInterval)timeIntervalSince1970;
        //以1970/01/01 GMT为基准时间,返回实例保存的时间与1970/01/01 GMT的时间间隔
        NSDate *TiDate3=[NSDate dateWithString:@"1970-01-01 00:10:00 +0000"];
        NSLog(@"%g",[TiDate3 timeIntervalSince1970]);//600
        
        // - (NSTimeInterval)timeIntervalSinceReferenceDate;
        //以2001/01/01 GMT为基准时间,返回实例保存的时间与2001/01/01 GMT的时间间隔
        NSDate *TiDate4=[NSDate dateWithString:@"2001-01-01 00:10:00 +0000"];
        NSLog(@"%g",[TiDate4 timeIntervalSinceReferenceDate]);//600
        
        //+ (NSTimeInterval)timeIntervalSinceReferenceDate;
        //以2001/01/01 GMT为基准时间,返回当前时间(Now)与2001/01/01 GMT的时间间隔
        NSLog(@"%g",[NSDate timeIntervalSinceReferenceDate]);//3.98737e+08
  
        
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值