1 // 当前时间创建NSDate
NSDate *myDate = [NSDate date];
NSLog(@"myDate = %@",myDate);
2 //从现在开始的24小时
NSTimeInterval secondsPerDay = 24*60*60;
NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];
NSLog(@"myDate = %@",tomorrow);
3//根据已有日期创建日期
NSTimeInterval secondsPerDay1 = 24*60*60;
NSDate *now = [NSDate date];
NSDate *yesterDay = [now addTimeInterval:-secondsPerDay1];
NSLog(@"yesterDay = %@",yesterDay);
4//比较日期
BOOL sameDate = [now isEqualToDate:yesterDay];
NSLog(@"sameDate = %lu",sameDate);
4.1//获取较早的日期
NSDate *earlierDate = [yesterDay earlierDate:now];
NSLog(@"earlierDate = %@",earlierDate);
4.2//较晚的日期
NSDate *laterDate = [yesterDay laterDate:now];
NSLog(@"laterDate = %@",laterDate);
//两个日期之间相隔多少秒
NSTimeInterval secondsBetweenDates= [yesterDay timeIntervalSinceDate:now];
NSLog(@"secondsBetweenDates= %lf",secondsBetweenDates);
//通过NSCALENDAR类来创建日期
NSDateComponents *comp = [[NSDateComponentsalloc]init];
[comp setMonth:06];
[comp setDay:01];
[comp setYear:2001];
NSCalendar *myCal = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *myDate1 = [myCal dateFromComponents:comp];
NSLog(@"myDate1 = %@",myDate1);
//从已有日期获取日期
unsigned units = NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit;
NSDateComponents *comp1 = [myCal components:units fromDate:now];
NSInteger month = [comp1 month];
NSInteger year = [comp1 year];
NSInteger day = [comp1 day];
//NSDateFormatter实现日期的输出
NSDateFormatter *formatter = [[NSDateFormatteralloc]init];
[formatter setDateStyle:NSDateFormatterFullStyle];//直接输出的话是机器码
//或者是手动设置样式[formatter setDateFormat:@"yyyy-mm-dd"];
NSString *string = [formatter stringFromDate:now];
NSLog(@"string = %@",string);
NSLog(@"formater = %@",formatter);
NSDateFormatter日期的格式化
1、基础用法
1 NSDate* now = [NSDate date]; 2 NSDateFormatter* fmt = [[NSDateFormatter alloc] init]; 3 fmt.dateStyle = kCFDateFormatterShortStyle; 4 fmt.timeStyle = kCFDateFormatterShortStyle; 5 fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; 6 NSString* dateString = [fmt stringFromDate:now]; 7 NSLog(@"%@", dateString);
打印输出:10/29/12, 2:27 PM
这使用的系统提供的格式化字符串,通过 fmt.dateStyle 和 fmt.timeStyle 进行的设置。实例中使用的参数是 kCFDateFormatterShortStyle,此外还有:
typedef CF_ENUM(CFIndex, CFDateFormatterStyle) { // date and time format styles kCFDateFormatterNoStyle = 0, // 无输出 kCFDateFormatterShortStyle = 1, // 10/29/12, 2:27 PM kCFDateFormatterMediumStyle = 2, // Oct 29, 2012, 2:36:59 PM kCFDateFormatterLongStyle = 3, // October 29, 2012, 2:38:46 PM GMT+08:00 kCFDateFormatterFullStyle = 4 // Monday, October 29, 2012, 2:39:56 PM China Standard Time };
2. 自定义区域语言
如上实例中,我们使用的是区域语言是 en_US,指的是美国英语。如果我们换成简体中文,则代码是:
1 fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
则对应的输出为:
typedef CF_ENUM(CFIndex, CFDateFormatterStyle) { // date and time format styles kCFDateFormatterNoStyle = 0, // 无输出 kCFDateFormatterShortStyle = 1, // 12-10-29 下午2:52 kCFDateFormatterMediumStyle = 2, // 2012-10-29 下午2:51:43 kCFDateFormatterLongStyle = 3, // 2012年10月29日 GMT+0800下午2时51分08秒 kCFDateFormatterFullStyle = 4 // 2012年10月29日星期一 中国标准时间下午2时46分49秒 };
世界通用的区域语言代码,详见 International Components for Unicode (ICU), http://userguide.icu-project.org/formatparse/datetime
3. 自定义日期时间格式
NSDateFormatter提供了自定义日期时间的方法,主要是通过设置属性 dateFormat,常见的设置如下:
1 NSDate* now = [NSDate date]; 2 NSDateFormatter* fmt = [[NSDateFormatter alloc] init]; 3 fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]; 4 fmt.dateFormat = @"yyyy-MM-dd'T'HH:mm:ss"; 5 NSString* dateString = [fmt stringFromDate:now]; 6 NSLog(@"%@", dateString);
打印输出:2012-10-29T16:08:40
除了上面列出的,还可以指定很多格式,详见http://userguide.icu-project.org/formatparse/datetime。
结合设置Locale,还可以打印出本地化的字符串信息。
1 NSDate* now = [NSDate date]; 2 NSDateFormatter* fmt = [[NSDateFormatter alloc] init]; 3 fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]; 4 fmt.dateFormat = @"yyyy-MM-dd a HH:mm:ss EEEE"; 5 NSString* dateString = [fmt stringFromDate:now]; 6 NSLog(@"\n%@", dateString);
打印输出:2012-10-29 下午 16:25:27 星期一
4. 自定义月份星期等字符
NSDateFormatter中同样提供了相应的方式,去修改这些字符。一般情况下,使用相应区域语言下面的默认字符就OK了。但是你的确有这个需求,那么也是可以办到的。相应的方法非常多,如下:
Managing AM and PM Symbols
- – AMSymbol
- – setAMSymbol:
- – PMSymbol
- – setPMSymbol:
Managing Weekday Symbols
- – weekdaySymbols
- – setWeekdaySymbols:
- – shortWeekdaySymbols
- – setShortWeekdaySymbols:
- – veryShortWeekdaySymbols
- – setVeryShortWeekdaySymbols:
- – standaloneWeekdaySymbols
- – setStandaloneWeekdaySymbols:
- – shortStandaloneWeekdaySymbols
- – setShortStandaloneWeekdaySymbols:
- – veryShortStandaloneWeekdaySymbols
- – setVeryShortStandaloneWeekdaySymbols:
Managing Month Symbols
- – monthSymbols
- – setMonthSymbols:
- – shortMonthSymbols
- – setShortMonthSymbols:
- – veryShortMonthSymbols
- – setVeryShortMonthSymbols:
- – standaloneMonthSymbols
- – setStandaloneMonthSymbols:
- – shortStandaloneMonthSymbols
- – setShortStandaloneMonthSymbols:
- – veryShortStandaloneMonthSymbols
- – setVeryShortStandaloneMonthSymbols:
Managing Quarter Symbols
- – quarterSymbols
- – setQuarterSymbols:
- – shortQuarterSymbols
- – setShortQuarterSymbols:
- – standaloneQuarterSymbols
- – setStandaloneQuarterSymbols:
- – shortStandaloneQuarterSymbols
- – setShortStandaloneQuarterSymbols:
Managing Era Symbols
- – eraSymbols
- – setEraSymbols:
- – longEraSymbols
- – setLongEraSymbols: