将本地NSDate时间与PST时间运行时间进行比较(Compare local NSDate time to PST time operating hours)

编程入门 行业动态 更新时间:2024-10-17 04:41:05
将本地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; }

}

My app has a chat component where users talk directly to customer service representatives and I want to make sure to notify the user if they are requesting help during office hours.

The office hours are in pst from 9 am to 7 pm.

Here is my current code to display a notification to the user if the office is closed, but it is not working properly.

- (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其他属性。

If all you care about is whether it's currently between 9AM and 7PM PST, you can do it a lot more easily than that. Just get an NSDateComponents for the current time, in PST, and look at the hour property of the result.

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"); }

If you also care about the day of week or other details, look at other properties of NSDateComponents.

更多推荐

本文发布于:2023-08-03 13:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390676.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   Compare   PST   NSDate   operating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!