您的位置:首页 > 大数据 > 人工智能

Snail—OC学习之日期NSDate

2015-07-25 15:48 477 查看
#import <Foundation/Foundation.h>

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

@autoreleasepool {

//得到现在的时间 2015-07-25 07:31:34  格林尼治时间
NSDate * now = [NSDate date];
NSDate * now1 = [NSDate new];
//返回当前时间多少秒后的时间
NSDate * date1 = [NSDate dateWithTimeIntervalSinceNow:10];
//返回1970年多少秒后的时间
NSDate * date2 = [NSDate dateWithTimeIntervalSince1970:10];
//返回2001年多少秒后的时间
NSDate * date3 = [NSDate dateWithTimeIntervalSinceReferenceDate:10];
//返回一个 过去很久远的时间 0001-12-30 00:00:00 +0000
NSDate * date4 = [NSDate distantPast];
//返回一个 未来很久远的时间 4001-01-01 00:00:00 +0000
NSDate * date5 = [NSDate distantFuture];
//在指定日期上加时间
[now dateByAddingTimeInterval:3600];
//时间间隔 返回的是秒数 float
NSTimeInterval s1 = [date1 timeIntervalSinceNow];
//与1970.1.1日距离的时间间隔
NSTimeInterval s2 = [date1 timeIntervalSince1970];
//与2001.1.1日距离的时间间隔
NSTimeInterval s3 = [date1 timeIntervalSinceReferenceDate];
//返回两个时间的间隔
NSTimeInterval s4 = [date1 timeIntervalSinceDate:now];
//比较两个日期大小 返回值跟字符串比较的返回值相同 NSComparisonResult
[now compare:date2];
//查看系统时区
NSTimeZone * timeZone = [NSTimeZone systemTimeZone];
//得到所有时区 返回值是NSDictionary
NSDictionary * dict = [NSTimeZone abbreviationDictionary];

//格式化对应的字母符号
/*
y:年
M:月
d:日
H:时
m:分
s:秒
SS:毫秒
*/
//利用时间戳来格式化输出时间
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy年MM月dd日 HH:mm:s.SS";
//更改时区
formatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"HKT"];
NSLog(@"%@",[formatter stringFromDate:now]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: