将本地 NSDate 时间与 PST 时间运行时间进行比较
Compare local NSDate time to PST time operating hours
我的应用程序有一个聊天组件,用户可以在其中直接与客户服务代表交谈,如果他们在办公时间请求帮助,我想确保通知用户。
办公时间为太平洋标准时间上午 9 点至晚上 7 点。
这是我当前的代码,用于在办公室关闭时向用户显示通知,但它无法正常工作。
- (void)checkOfficeHours {
//set opening hours date
NSDateComponents *openingTime = [[NSDateComponents alloc] init];
openingTime.hour = 9;
openingTime.minute = 0;
//set closing time hours
NSDateComponents *closingTime = [[NSDateComponents alloc] init];
closingTime.hour = 19;
closingTime.minute = 0;
//get the pst time from local time
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat=@"hh:mm";
NSDate *currentDate = [NSDate date];
NSTimeZone *pstTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"PST"];
dateFormatter.timeZone = pstTimeZone;
NSString *pstTimeString = [dateFormatter stringFromDate:currentDate];
//convert pst date string back to date
NSDate *now = [dateFormatter dateFromString:pstTimeString];
//create the current date component
NSDateComponents *currentTime = [[NSCalendar currentCalendar] components:NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:now];
//sort the array by times
NSMutableArray *times = [@[openingTime, closingTime, currentTime] mutableCopy];
[times sortUsingComparator:^NSComparisonResult(NSDateComponents *t1, NSDateComponents *t2) {
if (t1.hour > t2.hour) {
return NSOrderedDescending;
}
if (t1.hour < t2.hour) {
return NSOrderedAscending;
}
// hour is the same
if (t1.minute > t2.minute) {
return NSOrderedDescending;
}
if (t1.minute < t2.minute) {
return NSOrderedAscending;
}
// hour and minute are the same
if (t1.second > t2.second) {
return NSOrderedDescending;
}
if (t1.second < t2.second) {
return NSOrderedAscending;
}
return NSOrderedSame;
}];
//if the current time is in between (index == 1) then its during office hours
if ([times indexOfObject:currentTime] == 1) {
NSLog(@"We are Open!");
self.officeHoursView.hidden = YES;
} else {
NSLog(@"Sorry, we are closed!");
self.officeHoursView.hidden = NO;
}
}
如果您只关心当前是否在太平洋标准时间上午 9 点到晚上 7 点之间,那么您可以轻松得多。只需在 PST 中获取当前时间的 NSDateComponents
,然后查看结果的 hour
属性。
NSTimeZone *pst = [NSTimeZone timeZoneWithName:@"PST"];
NSDateComponents *pstComponentsForNow = [[NSCalendar currentCalendar] componentsInTimeZone:pst fromDate:[NSDate date]];
if ((pstComponentsForNow.hour >= 9) && (pstComponentsForNow.hour <= 19)) {
NSLog(@"Open");
} else {
NSLog(@"Closed");
}
如果您还关心星期几或其他详细信息,请查看 NSDateComponents
的其他属性。
我的应用程序有一个聊天组件,用户可以在其中直接与客户服务代表交谈,如果他们在办公时间请求帮助,我想确保通知用户。
办公时间为太平洋标准时间上午 9 点至晚上 7 点。
这是我当前的代码,用于在办公室关闭时向用户显示通知,但它无法正常工作。
- (void)checkOfficeHours {
//set opening hours date
NSDateComponents *openingTime = [[NSDateComponents alloc] init];
openingTime.hour = 9;
openingTime.minute = 0;
//set closing time hours
NSDateComponents *closingTime = [[NSDateComponents alloc] init];
closingTime.hour = 19;
closingTime.minute = 0;
//get the pst time from local time
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat=@"hh:mm";
NSDate *currentDate = [NSDate date];
NSTimeZone *pstTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"PST"];
dateFormatter.timeZone = pstTimeZone;
NSString *pstTimeString = [dateFormatter stringFromDate:currentDate];
//convert pst date string back to date
NSDate *now = [dateFormatter dateFromString:pstTimeString];
//create the current date component
NSDateComponents *currentTime = [[NSCalendar currentCalendar] components:NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:now];
//sort the array by times
NSMutableArray *times = [@[openingTime, closingTime, currentTime] mutableCopy];
[times sortUsingComparator:^NSComparisonResult(NSDateComponents *t1, NSDateComponents *t2) {
if (t1.hour > t2.hour) {
return NSOrderedDescending;
}
if (t1.hour < t2.hour) {
return NSOrderedAscending;
}
// hour is the same
if (t1.minute > t2.minute) {
return NSOrderedDescending;
}
if (t1.minute < t2.minute) {
return NSOrderedAscending;
}
// hour and minute are the same
if (t1.second > t2.second) {
return NSOrderedDescending;
}
if (t1.second < t2.second) {
return NSOrderedAscending;
}
return NSOrderedSame;
}];
//if the current time is in between (index == 1) then its during office hours
if ([times indexOfObject:currentTime] == 1) {
NSLog(@"We are Open!");
self.officeHoursView.hidden = YES;
} else {
NSLog(@"Sorry, we are closed!");
self.officeHoursView.hidden = NO;
}
}
如果您只关心当前是否在太平洋标准时间上午 9 点到晚上 7 点之间,那么您可以轻松得多。只需在 PST 中获取当前时间的 NSDateComponents
,然后查看结果的 hour
属性。
NSTimeZone *pst = [NSTimeZone timeZoneWithName:@"PST"];
NSDateComponents *pstComponentsForNow = [[NSCalendar currentCalendar] componentsInTimeZone:pst fromDate:[NSDate date]];
if ((pstComponentsForNow.hour >= 9) && (pstComponentsForNow.hour <= 19)) {
NSLog(@"Open");
} else {
NSLog(@"Closed");
}
如果您还关心星期几或其他详细信息,请查看 NSDateComponents
的其他属性。