定时器与日历
NSDate
得到现在的date
//get current date at +0000
NSDate *date = [NSDate date];
NSLog(@"date:%@",date);
以字符串形式表示date
//get description with current locale
NSString *dateStr = [date descriptionWithLocale:[NSLocale currentLocale]];
NSLog(@"dateStr:%@",dateStr);
比较两个date,哪个时间早,哪个时间晚,算出时间差
//get earlier/later date ,get time minus
NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:100];
NSDate *date2 = [NSDate dateWithTimeIntervalSinceNow:-100];
NSDate *earlierDate = [date1 earlierDate:date2];
NSDate *laterDate = [date1 laterDate:date2];
NSUInteger timeSec = [date1 timeIntervalSinceDate:date2];
NSLog(@"\nearlierDate:%@\nlaterDate:%@\ntimeInterval:%lu",earlierDate,laterDate,timeSec);
将date转化成指定格式的字符串
//dateFormat
//string from date
NSDateFormatter *dateFormat = [NSDateFormatter new];
dateFormat.dateFormat = @"YYYY年MM月dd日 HH时mm分ss秒 EEEE";
NSString *formatStr = [dateFormat stringFromDate:date];
NSLog(@"formatStr:%@",formatStr);
将指定格式的字符串转化成date
//date from string
dateFormat.dateFormat = @"YYYY-MM-dd HH-mm-ss";
formatStr = @"2015-6-3 13-00-00";
NSDate *formatDate = [dateFormat dateFromString:formatStr];
NSLog(@"formatDate:%@",formatDate);
NSCalendar
从一个date中得到年、月、日、时、分、秒、星期等元素
//get date component from calendar
NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *dateComponent = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
NSLog(@"%lu year %lu month %lu day",dateComponent.year,dateComponent.month,dateComponent.day);
得到某个月有多少天
//how many days in date's month
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:[NSDate date]];
NSLog(@"range:(%lu,%lu)",range.location,range.length);
得到date是在当前年的第几周
//number weekdays in year
NSUInteger ordinal = [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitYear forDate:[NSDate date]];
NSLog(@"ordinal:%lu",ordinal);
从年、月、日、时、分、秒等元素,来创建一个date
//creat date from date components
NSDateComponents *comp = [NSDateComponents new];
comp.year = 2015;
comp.month = 6;
comp.day = 3;
NSDate *compDate = [calendar dateFromComponents:comp];
NSLog(@"compDate:%@",[compDate descriptionWithLocale:[NSLocale currentLocale]]);
基于一个date,来创建另外一个date;
//creat a date later date components
NSDateComponents *comp1 = [NSDateComponents new];
comp1.hour = 5;
NSDate *newDate = [calendar dateByAddingComponents:comp1 toDate:[NSDate date] options:0];
NSLog(@"newDate:%@",[newDate descriptionWithLocale:[NSLocale currentLocale]]);
计算从某一时刻起,到指定的date,有多少年、月、日等元素
//calculate how many months from 1970
NSDateComponents *comp2 = [calendar components:NSCalendarUnitMonth fromDate:[NSDate dateWithTimeIntervalSince1970:0] toDate:[NSDate date] options:0];
NSLog(@"%lu",comp2.month);
NSTimer
1、creat a timer and count since now
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(didTimerFired:) userInfo:nil repeats:YES];
上面就创建了一个timer,并且每间隔1.0秒就会自动调用一次[self didTimerFired:timer];
2、creat a timer and count after date that you specifid
//与上一段代码相比,类方法名少了个scheduled
NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(didTimerFired:) userInfo:nil repeats:YES];
//此处为手动触发一次Fire
[timer fire];
//设置fireDate,默认为当前date
[timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:10]];
//监控timer,如果没有下面这条语句,timer将不会调用fire
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
3、NSTimer 的精度只能达到50ms-100ms,相对较低,如果想要精度高的定时,可以用GCD