Objective-C NSDate

2013-12-12 17:57  1585人阅读  评论 (0)

当前时间

NSDate *date = [NSDate date];
NSLog(@"%@", date);

NSDate => long

NSLog(@"%f", (double)[date timeIntervalSince1970] );

long => NSDate

NSDate *date2 = [NSDate dateWithTimeIntervalSince1970:981180306]; // 981144306 981180306
NSLog(@"%@", date2);

NSDate => NSString

NSDateFormatter *df = [NSDateFormatter new];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [df stringFromDate:date2];
NSLog(@"%@", dateString);

NSString => NSDate

NSString *dateString2 = @"2001-02-03 16:05:06";
NSDateFormatter *df2 = [NSDateFormatter new];
[df2 setTimeZone:[NSTimeZone systemTimeZone]];
[df2 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date3 = [df2 dateFromString:dateString2];
NSLog(@"%@", [df stringFromDate:date3]);