由于最近项目需要,关于VIP截止时间的处理:项目需求时这样的:支付完成之后在某个界面上显示VIP的截止时间,VIP的时间是一个月,下面是代码
static NSString *const limitDate = @"limitDate";
static NSString *const formatPaymentTime = @"formatPaymentTime";
/**
* 保存截止时间
*
* @param paymentTime 支付时间
*
* @return 截止时间
*/
+ (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:NSGregorianCalendar];
//时间比较,这个目的是有可能续费,截至时间需要叠加
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是否过期
*
* @return YES:过期/NO:没过期
*/
+ (BOOL)isExpried{
NSCalendar *calender = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
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;
}
网友评论