你的位置:首页 > 操作系统

[操作系统]iOS开发日记26


今天博主有一个时间戳的需求,遇到了一些困难点,在此和大家分享,希望能够共同进步.

iOS开发中,我们在很多情况下都会遇到使用当下时间作为参数的情况,这称之为时间戳.在iOSSDK中,与时间戳相关的API主要有三个,NSDate,NSCalendar和NSDate​Components.

NSDate 就是一个绝对的时间,可以换算成相对1970s的时差.

NSCalendar 顾名思义就是日历,封装了系统如何按照年月日的方式来组织时间,组织时间的方式和地区,时区有很大关系.

NSDate​Components 只是一个容器,容器中详细包含了年月日时分等信息.

其中NSDate相信大家用的比较多,就不在这里赘述了,各位看官自行百度,今天主要和大家分享一下NSCalendar和NSDate​Components,下面举几个例子,并贴出代码,让大家能更好的理解.

1.拆分一个时间

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);

2.查看今天是在今年的第几周

NSCalendar * calendar = [NSCalendar currentCalendar];

NSDate * currentDate = [NSDate date];

NSInteger week = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitYear forDate:currentDate];

NSLog(@"今天是今年的第%ld周",week);

3.根据拆分时间返回NSDate

NSDateComponents * components = [[NSDateComponents alloc] init];

components.year = 2015;

components.month = 9;

components.day = 28;

components.hour = 14;

components.minute = 38;

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);

4.相对时间(现在往后11天10小时)

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);

5.这个月还有多少天

NSCalendar * calendar = [NSCalendar currentCalendar];

NSDate * currentDate = [NSDate date];

NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:currentDate];

6.补充

①.创建不同地区的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).