NSCalendar封裝了系統(tǒng)如何按照年月日的方式來(lái)組織時(shí)間,快速獲取日期對(duì)象的年月日扮碧、時(shí)分秒等相關(guān)信息。
1.獲取當(dāng)前時(shí)間
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitMinute | NSCalendarUnitMonth | NSCalendarUnitHour | NSCalendarUnitDay fromDate:[NSDate date]];
NSLog(@"%ld月%ld日%ld時(shí)%ld分" ,(long)components.month,(long)components.day,(long)components.hour,(long)components.minute);
2.查看今天是在今年的第幾周
NSCalendar *calendar1 = [NSCalendar currentCalendar];
NSInteger week = [calendar1 ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitYear forDate:[NSDate date]];
NSLog(@"今天是今年的第%ld周",(long)week);
3.現(xiàn)在往后3天2小時(shí)
NSDateComponents * components2 = [[NSDateComponents alloc] init];
components2.year = 0;
components2.day = 3;
components2.hour = 2;
NSCalendar *calendar3 = [NSCalendar currentCalendar];
NSDate *currentDate = [NSDate date];
NSDate *nextData = [calendar3 dateByAddingComponents:components2 toDate:currentDate options:NSCalendarMatchStrictly];
NSDateFormatter * formatter1 = [[NSDateFormatter alloc] init];
formatter1.dateFormat = @"yyyy年MM月dd日hh時(shí)mm分ss秒";
NSString * str1 = [formatter1 stringFromDate:nextData];
NSLog(@"%@",str1);
下面通過(guò)兩個(gè)常用的小例子來(lái)實(shí)現(xiàn)
1.需求是一天執(zhí)行一次某個(gè)操作
- (void)setToday:(NSString *)userDefaultsName{
//存儲(chǔ)當(dāng)前時(shí)間
NSCalendar *calendar0 = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar0 components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:[NSDate date]];
NSInteger currentIndex = [[NSString stringWithFormat:@"%ld%ld%ld",(long)components.year,(long)components.month,(long)components.day] integerValue];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setInteger:currentIndex forKey:userDefaultsName];
[userDefaults synchronize];
}
//檢測(cè)userDefaultsName對(duì)應(yīng)的時(shí)間和當(dāng)前時(shí)間相比是否已經(jīng)超出了一天 yes為已經(jīng)超出
+(BOOL)exceedOneDay:(NSString *)userDefaultsName{
//取出保存的時(shí)間和當(dāng)前時(shí)間進(jìn)行比較看是否超出一天
NSCalendar *calendar0 = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar0 components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitMonth) fromDate:[NSDate date]];
NSInteger todayTimeInDay = [[NSString stringWithFormat:@"%ld%ld%ld",components.year,components.month,components.day]integerValue];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSInteger lastTimeInDay = [userDefaults integerForKey:userDefaultsName];
if (todayTimeInDay>lastTimeInDay) {
//超出一天
return YES;
}
return NO;
}
2.在某個(gè)界面上顯示VIP的截止時(shí)間,VIP的時(shí)間是一個(gè)月
static NSString *const limitDate = @"limitDate";
static NSString *const formatPaymentTime = @"formatPaymentTime";
+ (NSString *)persistLimitTime:(NSString *)paymentTime{
//加這一句的目的是因?yàn)樵谥Ц稌r(shí)候本地的時(shí)間格式是下面的樣子浩淘,
//由于項(xiàng)目需求要用到Y(jié)YYY-MM-dd這種格式
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYYMMddHHmmss"];
NSDate *currentPayTime = [formatter dateFromString:paymentTime];
NSDate *recordDate = [[NSUserDefaults standardUserDefaults] objectForKey:limitDate];
[formatter setDateFormat:@"YYYY-MM-dd"];
formatter.timeZone = [NSTimeZone defaultTimeZone];
NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
//時(shí)間比較,這個(gè)目的是有可能續(xù)費(fèi)吴攒,截至?xí)r間需要疊加
if (recordDate) {
NSComparisonResult result = [calender compareDate:currentPayTime toDate:recordDate toUnitGranularity:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay];
if (result==NSOrderedAscending) {
currentPayTime = recordDate;//截止時(shí)間大于當(dāng)前支付時(shí)間
}
}
//加一個(gè)月张抄,VIP 周期
NSDateComponents *monthComponent = [[NSDateComponents alloc] init];
[monthComponent setMonth:+1];
[monthComponent setYear:0];
[monthComponent setDay:0];
NSDate *toDate = [calender dateByAddingComponents:monthComponent toDate:currentPayTime options:0];
NSDateComponents *component = [calender components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:toDate];
NSDate *dateNew = [calender dateFromComponents:component];
paymentTime = [formatter stringFromDate:dateNew];
[[NSUserDefaults standardUserDefaults] setObject:dateNew forKey:limitDate];
[[NSUserDefaults standardUserDefaults] setObject:paymentTime forKey:formatPaymentTime];
[[NSUserDefaults standardUserDefaults] synchronize];
return paymentTime;
}
//判斷VIP是否過(guò)期 YES:過(guò)期/NO:沒(méi)過(guò)期
+ (BOOL)isExpried{
NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *recordDate = [[NSUserDefaults standardUserDefaults] objectForKey:limitDate];
//時(shí)間比較
if (recordDate) {
NSComparisonResult result = [calender compareDate:[NSDate date] toDate:recordDate toUnitGranularity:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay];
return result==NSOrderedDescending?YES:NO;
}
return YES;
}
以上有不足之處還望指點(diǎn)。
參考鏈接http://www.reibang.com/p/75bb333a7582
更多有關(guān)NSCalendar使用請(qǐng)參考http://www.ithao123.cn/content-10808531.html