判断当前时间是否在某个时间段之内
用于直播项目 判断直播是已经开始 未开始 还是已经结束
// 判断时间 看直播是不是开始了
-(NSString *)NowtimeIsInBeginTime:(NSString *)begin
withEndTime:(NSString *)end{
// 创建日期格式化对象
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//样式
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
//格式
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
//时区
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
//字符串转时间
NSDate* begintime = [formatter dateFromString:begin];
NSDate* endtime = [formatter dateFromString:end];
//开始时间戳
NSTimeInterval begintimes= [ begintime timeIntervalSince1970] * 1000;
//当前时间戳
NSTimeInterval nowtimes = [ [NSDate date ] timeIntervalSince1970] * 1000;
//结束时间戳
NSTimeInterval endtiiems = [ endtime timeIntervalSince1970] * 1000;
if (begintimes< nowtimes && nowtimes< endtiiems) {
return @"已经开始";
}else{
return @"未开始";
}
}
返回的字符串 也可改为bool
```
NSString * begin = [self NowtimeIsInBeginTime:@"2016-12-14 09:00:00" withEndTime:@"2017-12-14 22:00:00"];
```