标签:
02-比较日期
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) 4 { 5 6 @autoreleasepool { 7 8 NSTimeInterval secondes = 24 * 60 * 60; 9 10 NSDate * now = [NSDate date]; 11 12 //模拟昨天 24 * 60 * 60 13 NSDate * yesDay = [now addTimeInterval:-secondes]; 14 15 //比较两个日期是否相同 16 17 BOOL isEqual = [now isEqualToDate:yesDay]; 18 if (isEqual) 19 { 20 NSLog(@"相同"); 21 } 22 else 23 { 24 NSLog(@"不相同"); 25 } 26 27 //获得两个日期中比较早的一个 28 NSDate * earler = [yesDay earlierDate:now]; 29 NSLog(@"%@",earler); 30 31 NSDate * later = [yesDay laterDate:now]; 32 NSLog(@"later %@",later); 33 34 } 35 return 0; 36 }
03-格式化日期
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) 4 { 5 6 @autoreleasepool { 7 8 /*格式化日期操作 9 NSDate * date = [NSDate date]; 10 11 //表示格式化日期对象 12 NSDateFormatter * f = [[NSDateFormatter alloc] init]; 13 14 [f setDateFormat:@"yyyy-MM-dd hh:mm:ss"];//2014/03/21 15 16 //把格式与我们的日期关联起来 17 NSString * str = [f stringFromDate:date]; 18 NSLog(@"str %@",str); 19 */ 20 21 //把字符串转换成日期 22 /* 23 NSString * str = @"2014/03/20"; 24 25 NSDateFormatter * f = [[NSDateFormatter alloc] init]; 26 [f setDateFormat:@"yyyy/MM/dd"]; 27 NSDate * date = [f dateFromString:str]; 28 NSLog(@"date %@",date); 29 */ 30 31 //处理时区问题 32 33 NSDate * newDate = [NSDate date]; 34 35 NSDateFormatter * f = [[NSDateFormatter alloc] init]; 36 [f setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 37 38 NSString * str = [f stringFromDate:newDate]; 39 //设置时差计算方法 //GTM 40 [f setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]]; 41 42 NSLog(@"%@",str); 43 44 } 45 return 0; 46 }
04-日期操作
1 #import <Foundation/Foundation.h> 2 3 int main(int argc, const char * argv[]) 4 { 5 6 @autoreleasepool { 7 8 //获得日期对象 9 NSDate * date = [NSDate date]; 10 NSLog(@"date %@",date); 11 12 } 13 return 0; 14 }
标签:
原文地址:http://www.cnblogs.com/chentaiyan/p/4627331.html