原创Blog,转载请注明出处
blog.csdn.net/hello_hwc
欢饮关注我的iOS SDK详解专栏
http://blog.csdn.net/column/details/huangwenchen-ios-sdk.html
前言:NSCalendar 和 NSDate?Components是有关iOS 时间相关API很重要的两个类。最近刚好用到,这里就整理下。
概念
- NSCalendar 顾名思义就是日历,封装了系统如何按照年月日的方式来组织时间,组织时间的方式和地区,时区有很大关系
- NSDate?Components 只是一个容器,容器中详细包含了年月日时分等信息。
- NSDate 就是一个绝对的时间,可以换算成相对1970s的时差。
举例
拆分一个时间
OC
NSDate * date = [NSDate date];//当前时间
NSCalendar * calendar = [NSCalendar currentCalendar];//当前用户的calendar
NSDateComponents * components = [calendar components:NSCalendarUnitMinute | NSCalendarUnitMonth | NSCalendarUnitHour | NSCalendarUnitDay fromDate:date];
NSLog(@"%ld月%ld日%ld时%ld分" ,(long)components.month,(long)components.day,(long)components.hour,(long)components.minute);
Swift
let date = NSDate()
let calendar = NSCalendar.currentCalendar()
let components = calendar.components(NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitMinute, fromDate:date)
NSLog("%ld月%ld日%ld时%ld分" ,components.month, components.day, components.hour, components.minute)
相对序号
例如,查看今天是在今年的第几周
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDate * currentDate = [NSDate date];
NSInteger week = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitYear forDate:currentDate];
NSLog(@"今天是今年的第%ld周",week);
根据拆分时间返回NSDate
OC
NSDateComponents * components = [[NSDateComponents alloc] init];
components.year = 2015;
components.month = 7;
components.day = 11;
components.hour = 10;
components.minute = 30;
components.second = 20;
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDate * date = [calendar dateFromComponents:components];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy年MM月dd日hh时mm分ss秒";
NSString * str = [formatter stringFromDate:date];
NSLog(@"%@",str);
Swift
let components = NSDateComponents()
components.year = 2015
components.month = 7
components.day = 11
components.hour = 10
components.minute = 30
components.second = 20
let calendar = NSCalendar.currentCalendar()
let date = calendar.dateFromComponents(components)
let formatter = NSDateFormatter();
formatter.dateFormat = "yyyy年MM月dd日hh时mm分ss秒";
let str = formatter.stringFromDate(date!);
println(str)
相对时间
现在往后11天10小时
OC
NSDateComponents * components = [[NSDateComponents alloc] init];
components.day = 11;
components.hour = 10;
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDate * currentDate = [NSDate date];
NSDate * nextData = [calendar dateByAddingComponents:components toDate:currentDate options:NSCalendarMatchStrictly];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy年MM月dd日hh时mm分ss秒";
NSString * str = [formatter stringFromDate:nextData];
NSLog(@"%@",str);
Swift
let components = NSDateComponents()
components.day = 11;
components.hour = 10;
let calendar = NSCalendar.currentCalendar();
let currentDate = NSDate()
let nextDate = calendar.dateByAddingComponents(components, toDate: currentDate, options:NSCalendarOptions.MatchStrictly)
let formatter = NSDateFormatter();
formatter.dateFormat = "yyyy年MM月dd日hh时mm分ss秒";
let str = formatter.stringFromDate(nextDate!)
NSLog("%@",str);
这个月有多少天
NSCalendar * calendar = [NSCalendar currentCalendar];
NSDate * currentDate = [NSDate date];
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:currentDate];
其他
创建不同地区的NSCalendar
initWithCalendarIdentifier:
其中有效的identifer
NSString * const NSGregorianCalendar;
NSString * const NSBuddhistCalendar;
NSString * const NSChineseCalendar;
NSString * const NSHebrewCalendar;
NSString * const NSIslamicCalendar;
NSString * const NSIslamicCivilCalendar;
NSString * const NSJapaneseCalendar;
NSString * const NSRepublicOfChinaCalendar;
NSString * const NSPersianCalendar;
NSString * const NSIndianCalendar;
NSString * const NSISO8601Calendar;
一周的开始日期
– firstWeekday
时区和地区
locale
timeZone
所有的NSCalendarUnit
NSEraCalendarUnit = kCFCalendarUnitEra,
NSYearCalendarUnit = kCFCalendarUnitYear,
NSMonthCalendarUnit = kCFCalendarUnitMonth,
NSDayCalendarUnit = kCFCalendarUnitDay,
NSHourCalendarUnit = kCFCalendarUnitHour,
NSMinuteCalendarUnit = kCFCalendarUnitMinute,
NSSecondCalendarUnit = kCFCalendarUnitSecond,
NSWeekCalendarUnit = kCFCalendarUnitWeek,
NSWeekdayCalendarUnit = kCFCalendarUnitWeekday,
NSWeekdayOrdinalCalendarUnit = kCFCalendarUnitWeekdayOrdinal,
NSQuarterCalendarUnit = kCFCalendarUnitQuarter,
NSWeekOfMonthCalendarUnit = kCFCalendarUnitWeekOfMonth,
NSWeekOfYearCalendarUnit = kCFCalendarUnitWeekOfYear,
NSYearForWeekOfYearCalendarUnit = kCFCalendarUnitYearForWeekOfYear
NSCalendarCalendarUnit = (1 << 20),
NSTimeZoneCalendarUnit = (1 << 21),
版权声明:本文为博主原创文章,如需转载请注明出处
时间: 2024-12-20 00:04:32