最近做一个限时抢购,根据当前时间判断当前活动的状态,最好是用服务器返回的时间,因为本地时间用户可以更改,中间去掉“T”是因为服务器返回的 2016-12-13T12:00:00
NSDate *currentDate = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.locale = [NSLocale localeWithLocaleIdentifier:@"en_US"];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSString * beginString=[self.GetBeginAt stringByReplacingOccurrencesOfString:@"T" withString:@" "];
NSDate *beginDate=[formatter dateFromString:beginString];
NSString * endString =[self.GetEndAt stringByReplacingOccurrencesOfString:@"T" withString:@" "];
NSDate *endDate=[formatter dateFromString:endString];
if ([self isBetweenFromdate:beginDate todate:endDate]) {
//在这个时间段就是:拼抢中
}else if([currentDate compare:endDate]==NSOrderedDescending) {
//比这个时间段大:已结束
}else {
//比这个时间段小:即将开始
}
- (BOOL)isBetweenFromdate:(NSDate *)dateFrom todate:(NSDate *)dateTo {
NSDate *currentDate = [NSDate date];
if ([currentDate compare:dateFrom]==NSOrderedDescending && [currentDate compare:dateTo]==NSOrderedAscending) {
//比开始时间大,比结束时间小
return YES;
}
return NO;
}
判断两个时间是不是同一天,判断优惠券是不是当天使用,传入两个日期字符串
// 判断是否是同一天
+ (BOOL)isSameDay:(NSString *)dateStr1 date2:(NSString *)dateStr2
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date1 = [dateFormatter dateFromString:dateStr1];
NSDate *date2 = [dateFormatter dateFromString:dateStr2];
NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlag = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *comp1 = [calendar components:unitFlag fromDate:date1];
NSDateComponents *comp2 = [calendar components:unitFlag fromDate:date2];
return (([comp1 day] == [comp2 day]) && ([comp1 month] == [comp2 month]) && ([comp1 year] == [comp2 year]));
}
获取两个时间差,传入两个时间字符串
+(NSTimeInterval)getSecondsFrom:(NSString *)fDate to:(NSString *)tDate
{
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date1=[dateFormatter dateFromString:fDate];
NSDate *date2=[dateFormatter dateFromString:tDate];
//这里的NSTimeInterval 并不是对象,是基本型,其实是double类型,是由c定义的:typedef double NSTimeInterval;
NSTimeInterval time=[date2 timeIntervalSinceDate:date1];
return time;
}
即将过期是否显示,需求是还有七天 就显示即将过期,overdueImage是即将过期的图片
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
formater.dateFormat = @"yyyy-MM-dd hh:mm:ss";
NSDate *Date = [formater dateFromString:date];
NSDate *currentDate = [NSDate date];
double tempTimeInterval = [Date timeIntervalSinceDate:currentDate];
int day=3600*24;
int temp=tempTimeInterval/day;
if(temp<= 7 && temp>=0){
self.overdueImage.hidden=NO;
}else{
self.overdueImage.hidden=YES;
}
将时间转换为本地时间
+ (NSDate *)getNowDateFromatAnDate:(NSDate *)anyDate
{
//设置源日期时区
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];//或GMT
//设置转换后的目标日期时区
NSTimeZone* destinationTimeZone = [NSTimeZone localTimeZone];
//得到源日期与世界标准时间的偏移量
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:anyDate];
//目标日期与本地时区的偏移量
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:anyDate];
//得到时间偏移量的差值
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
//转为现在时间
NSDate* destinationDateNow = [[NSDate alloc] initWithTimeInterval:interval sinceDate:anyDate];
return destinationDateNow;
}
dateFromString 为 nil
通常情况是 dateformat 设置的原因,
formater.dateFormat = @"yyyy-MM-dd HH:mm:ss";
formater.dateFormat = @"yyyy-MM-dd hh:mm:ss";
大写“H”返回的是24小时,小写”h“返回的是12小时,