判斷當(dāng)前時(shí)間是否在某個(gè)時(shí)間段之內(nèi)?
用于直播項(xiàng)目 判斷直播是已經(jīng)開始 未開始 還是已經(jīng)結(jié)束
// 判斷時(shí)間? 看直播是不是開始了
-(NSString *)NowtimeIsInBeginTime:(NSString *)begin
withEndTime:(NSString *)end{
// 創(chuàng)建日期格式化對(duì)象
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//樣式
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
//格式
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
//時(shí)區(qū)
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
//字符串轉(zhuǎn)時(shí)間
NSDate* begintime = [formatter dateFromString:begin];
NSDate* endtime = [formatter dateFromString:end];
//開始時(shí)間戳
NSTimeInterval begintimes= [ begintime timeIntervalSince1970] * 1000;
//當(dāng)前時(shí)間戳
NSTimeInterval nowtimes = [ [NSDate date ] timeIntervalSince1970] * 1000;
//結(jié)束時(shí)間戳
NSTimeInterval endtiiems = [ endtime timeIntervalSince1970] * 1000;
if (begintimes< nowtimes && nowtimes< endtiiems) {
return @"已經(jīng)開始";
}else{
return @"未開始";
}
}
返回的字符串 也可改為bool?
```
NSString * begin = [self NowtimeIsInBeginTime:@"2016-12-14 09:00:00" withEndTime:@"2017-12-14 22:00:00"];
```