logo头像

Renbo’s Blog

iOS中关于日历和日期的计算

1.获取当前时间

NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents  *components  =  [calendar components:NSCalendarUnitMinute | NSCalendarUnitMonth | NSCalendarUnitHour | NSCalendarUnitDay fromDate:[NSDate date]];
NSLog(@"%ld月%ld日%ld时%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.现在往后3天2小时

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时mm分ss秒";
NSString * str1 = [formatter1 stringFromDate:nextData];
NSLog(@"%@",str1);

4.需求:一天执行一次某个操作

- (void)setToday:(NSString *)userDefaultsName{
// 存储当前时间
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];
}

// 检测userDefaultsName对应的时间和当前时间相比是否已经超出了一天 yes为已经超出
+(BOOL)exceedOneDay:(NSString *)userDefaultsName{
// 取出保存的时间和当前时间进行比较看是否超出一天
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;
}

5.在某个界面上显示VIP的截止时间,VIP的时间是一个月

static NSString *const limitDate = @"limitDate";
static NSString *const formatPaymentTime = @"formatPaymentTime";
+ (NSString *)persistLimitTime:(NSString *)paymentTime{

// 加这一句的目的是因为在支付时候本地的时间格式是下面的样子,
// 由于项目需求要用到YYYY-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];
// 时间比较,这个目的是有可能续费,截至时间需要叠加
if (recordDate) {
    NSComparisonResult result = [calender compareDate:currentPayTime toDate:recordDate toUnitGranularity:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay];
    if (result==NSOrderedAscending) {
        currentPayTime = recordDate;//截止时间大于当前支付时间
    }
}

// 加一个月,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是否过期  YES:过期/NO:没过期
+ (BOOL)isExpried {

NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDate *recordDate = [[NSUserDefaults standardUserDefaults] objectForKey:limitDate];

// 时间比较
if (recordDate) {
    NSComparisonResult result = [calender compareDate:[NSDate date] toDate:recordDate toUnitGranularity:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay];
    return result==NSOrderedDescending?YES:NO;

}
return YES;

}
上一篇