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

来源:互联网 发布:学中医的软件 编辑:程序博客网 时间:2024/05/17 07:09
  1. #import <Foundation/Foundation.h>  
  2.   
  3. int main(int argc, const char * argv[])  
  4. {  
  5.   
  6.     @autoreleasepool {  
  7.           
  8.         //NSDate 对象的初始化  
  9.         NSDate *nDate=[NSDate dateWithString:@"2013-08-06 08:09:10 +0000"];  
  10.         NSDate *nDate2=[NSDate dateWithString:@"2013-01-01 00:00:00 +0000"];  
  11.         NSLog(@"%@",nDate);// 输出 2013-08-06 08:09:10 +0000  
  12.           
  13.         // 以YYYY-MM-DD HH:MM:SS ±HHMM的格式表示时间。  
  14.         // 其中 "±HHMM" 表示与GMT的存在多少小时多少分钟的时区差异。比如,若时区设置在北京,则 "±HHMM" 显示为 "+0800"  
  15.           
  16.         /**********将 NSDate 转换 为 NSString **********/  
  17.         NSString *Str=[nDate description ];//  
  18.         NSLog(@"%@",Str);  
  19.           
  20.         /****************一些常用方法**********************************/  
  21.         // 方法:+ (id)date,返回当前时间  
  22.         NSDate*Date1=[NSDate date];  
  23.         NSLog(@"%@",Date1);//输出 XXXX-XX-XX XX:XX:XX +0000 格式  
  24.           
  25.         //方法:+ (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs  
  26.         //返回以当前时间为基准,然后过了secs秒的时间  
  27.         NSDate *Date2=[NSDate dateWithTimeIntervalSinceNow:600];  
  28.         NSLog(@"%@",Date2);  
  29.           
  30.         // + (id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;  
  31.         //返回以1970/01/01 GMT为基准,然后过了secs秒的时间  
  32.         NSDate *Date3=[NSDate dateWithTimeIntervalSince1970:600];  
  33.         NSLog(@"%@",Date3);//1970-01-01 00:10:00 +0000  
  34.           
  35.         //+ (id)distantFuture;  
  36.         //返回很多年以后的未来的某一天  
  37.         NSDate *Date4=[NSDate distantFuture];  
  38.         NSLog(@"%@",Date4);//4001-01-01 00:00:00 +0000  
  39.           
  40.         //+ (id)distantPast;  
  41.         //返回很多年以前的某一天。  
  42.         NSDate *Date5=[NSDate distantPast];  
  43.         NSLog(@"Date5:%@",Date5);//0001-12-30 00:00:00 +0000  
  44.           
  45.         //- (id)addTimeInterval:(NSTimeInterval)secs;  
  46.         //返回以目前的实例中保存的时间为基准,然后过了secs秒的时间  
  47.         NSDate *Date6=[nDate addTimeInterval:600];//有问题 请大神指点  
  48.         NSLog(@"Date6:%@",Date6);//Date6:2013-08-06 08:19:10 +0000  
  49.           
  50.         // - (id)init;  
  51.         //初始化为当前时间。类似date方法  
  52.         NSDate *Date7=[Date1 init];//等于Date1  
  53.         NSLog(@"Date7:%@",Date7);  
  54.           
  55.         //- (id)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;  
  56.         // 初始化为以2001/01/01 GMT为基准,然后过了secs秒的时间。类似dateWithTimeIntervalSinceReferenceDate:方法  
  57.         NSDate *Date8=[Date2 initWithTimeIntervalSinceReferenceDate:600];  
  58.         NSLog(@"%@",Date8);  // 2001-01-01 00:10:00 +0000  
  59.           
  60.         //  - (id)initWithTimeInterval:(NSTimeInterval)secs sinceDate:(NSDate *)refDate;  
  61.         //初始化为以refDate为基准,然后过了secs秒的时间  
  62.         NSDate *Date9=[Date1 initWithTimeInterval:600 sinceDate:[NSDate dateWithString:@"2013-08-06 08:39:10 +0000"]];  
  63.         NSLog(@"%@",Date9);//2013-08-06 08:49:10 +0000  
  64.           
  65.         /***********日期之间比较 方法*************/  
  66.           
  67.         //- (BOOL)isEqualToDate:(NSDate *)otherDate;  
  68.         //与otherDate比较,相同返回YES  
  69.         if ([nDate isEqualToDate:nDate2]==NO) {  
  70.             NSLog(@"这两个时间 不相同");  
  71.         }  
  72.         else  
  73.         {  
  74.             NSLog(@"这两个时间 相同");  
  75.         }  
  76.           
  77.         //- (NSDate *)earlierDate:(NSDate *)anotherDate;  
  78.         //与anotherDate比较,返回较早的那个日期  
  79.         NSLog(@"%@",[nDate earlierDate:nDate2]);//2013-01-01 00:00:00 +0000  
  80.           
  81.         // - (NSDate *)laterDate:(NSDate *)anotherDate;  
  82.         //与anotherDate比较,返回较晚的那个日期  
  83.         NSLog(@"%@",[nDate laterDate:nDate2]);//2013-08-06 08:09:10 +0000  
  84.           
  85.         /* 
  86.           
  87.          - (NSComparisonResult)compare:(NSDate *)other; 
  88.          该方法用于排序时调用: 
  89.          . 当实例保存的日期值与anotherDate相同时返回NSOrderedSame 
  90.          . 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending 
  91.          . 当实例保存的日期值早于anotherDate时返回NSOrderedAscending 
  92.           
  93.          */  
  94.           
  95.          /*******取回时间间隔可用以下方法******/  
  96.         //定义两个 NSDate  
  97.         NSDate *TiDate1=[NSDate dateWithString:@"2013-08-06 08:00:10 +0000"];  
  98.         NSDate *TiDate2=[NSDate dateWithString:@"2013-08-06 08:10:10 +0000"];  
  99.           
  100.         //- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate;  
  101.         //以refDate为基准时间,返回实例保存的时间与refDate的时间间隔  
  102.         NSLog(@"%f",[TiDate1 timeIntervalSinceDate:TiDate2]);//-600.000000  
  103.           
  104.         //- (NSTimeInterval)timeIntervalSinceNow;  
  105.         //以当前时间(Now)为基准时间,返回实例保存的时间与当前时间(Now)的时间间隔  
  106.         NSLog(@"%f",[TiDate1 timeIntervalSinceNow]);//-1267744.063120  
  107.           
  108.         // - (NSTimeInterval)timeIntervalSince1970;  
  109.         //以1970/01/01 GMT为基准时间,返回实例保存的时间与1970/01/01 GMT的时间间隔  
  110.         NSDate *TiDate3=[NSDate dateWithString:@"1970-01-01 00:10:00 +0000"];  
  111.         NSLog(@"%g",[TiDate3 timeIntervalSince1970]);//600  
  112.           
  113.         // - (NSTimeInterval)timeIntervalSinceReferenceDate;  
  114.         //以2001/01/01 GMT为基准时间,返回实例保存的时间与2001/01/01 GMT的时间间隔  
  115.         NSDate *TiDate4=[NSDate dateWithString:@"2001-01-01 00:10:00 +0000"];  
  116.         NSLog(@"%g",[TiDate4 timeIntervalSinceReferenceDate]);//600  
  117.           
  118.         //+ (NSTimeInterval)timeIntervalSinceReferenceDate;  
  119.         //以2001/01/01 GMT为基准时间,返回当前时间(Now)与2001/01/01 GMT的时间间隔  
  120.         NSLog(@"%g",[NSDate timeIntervalSinceReferenceDate]);//3.98737e+08  
  121.     
  122.           
  123.     }  
  124.     return 0;  
  125. }  
0 0
原创粉丝点击