NSDate

简介: 1 // 2 // main.m 3 // NSDate 4 // 5 // Created by dingxiaowei on 13-5-17.
复制代码
 1 //
 2 //  main.m
 3 //  NSDate
 4 //
 5 //  Created by dingxiaowei on 13-5-17.
 6 //  Copyright (c) 2013年 dingxiaowei. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 #pragma mark - 日期创建
12 void dateCreate(){
13     //NSData表示字节数组类型
14     //date方法就是返回当前的时间(now)
15     NSDate *date=[NSDate date];
16     date = [NSDate dateWithTimeIntervalSinceNow:10]; //该方法表示在当前时间的基础上增加10秒中
17     NSDate *date1=[NSDate date];
18     //从1970:1:1 00:00:00开始  那时候计算机出世  在这基础上加10秒
19     date1=[NSDate dateWithTimeIntervalSince1970:10];
20     NSLog(@"%@",date);
21     NSLog(@"%@",date1);
22 
23     //随机返回一个比较遥远的时间
24     date1=[NSDate distantFuture];
25     NSLog(@"%@",date1);
26 
27     //随即返回一个遥远的过去的时间
28     date1=[NSDate distantPast];
29     NSLog(@"%@",date1);
30 }
31 
32 #pragma mark - 日期类的应用
33 void dateUse(){
34     NSDate *date=[NSDate date];
35     NSTimeInterval sec=[date timeIntervalSince1970]; //跟1970年进行对比,返回时间差
36     NSLog(@"%zd",sec);
37     //跟一个时间进行对比
38     //[date timeIntervalSinceDate:<#(NSDate *)#>];
39     NSDate *date2=[NSDate date];
40     NSDate * dateEarlier=[date earlierDate:date2];
41     NSLog(@"比较早的那个时间是%@",dateEarlier);
42     NSDate * dateLater=[date laterDate:date2];
43     NSLog(@"比较晚的那个时间是%@",dateLater);
44 }
45 
46 
47 #pragma mark - 日期格式化
48 void dateFormat(){
49     NSDate *date=[NSDate date];
50     NSDateFormatter *formater=[[[NSDateFormatter alloc] init] autorelease];
51     //设置日期格式
52     formater.dateFormat=@"yyyy-MM-dd HH:mm:ss";  //HH大写代表24时制  hh代表12小时制
53     //把日期变成字符串
54     NSString *str=[formater stringFromDate:date];
55 
56     //设置时区
57     formater.locale=[[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"] autorelease];
58     //把字符串变成日期
59     //返回的是格林制时间
60     date=[formater dateFromString:@"2013-05-16 13:40:50"];
61     NSLog(@"字符串转化成日期是:%@",date);
62     NSLog(@"%@",str);
63 }
64 int main(int argc, const char * argv[])
65 {
66 
67     @autoreleasepool {
68         
69         dateCreate();
70         dateUse();
71         dateFormat();
72     }
73     return 0;
74 }
结果:
 
 

2013-08-10 13:51:46.178 NSDate[2019:303] 2013-08-10 05:51:56 +0000

2013-08-10 13:51:46.178 NSDate[2019:303] 1970-01-01 00:00:10 +0000

2013-08-10 13:51:46.179 NSDate[2019:303] 4001-01-01 00:00:00 +0000

2013-08-10 13:51:46.179 NSDate[2019:303] 0001-12-30 00:00:00 +0000

2013-08-10 13:51:46.179 NSDate[2019:303] 140735740346247

2013-08-10 13:51:46.180 NSDate[2019:303] 比较早的那个时间是2013-08-10 05:51:46 +0000

2013-08-10 13:51:46.180 NSDate[2019:303] 比较晚的那个时间是2013-08-10 05:51:46 +0000

2013-08-10 13:51:46.182 NSDate[2019:303] 字符串转化成日期是:2013-05-16 05:40:50 +0000

2013-08-10 13:51:46.182 NSDate[2019:303] 2013-08-10 13:51:46

相关文章
NSDate与NSDateFormatter的相关用法
NSDate与NSDateFormatter的相关用法
42 0
|
iOS开发
IOS学习笔记之十七 (NSDate、NSDateFormatter、NSCalendar、NSDateComponents、NSTimer)
IOS学习笔记之十七 (NSDate、NSDateFormatter、NSCalendar、NSDateComponents、NSTimer)
115 0
|
机器学习/深度学习 C语言 索引