使用 NSDate 根据一周中的通用天数计算时间

发布于 2024-09-08 08:08:59 字数 321 浏览 1 评论 0原文

我试图弄清楚如何根据通常的表达方式来计算商店开门时间。例如:周一至周五,上午 9 点至下午 6 点。我想从这个标准中了解两件事。商店开门了吗?如果是的话,距离关闭还有多长时间?

我对文档的困惑是 NSDate 计算期望唯一的日期数据集来执行不通用的计算,如上所述。例如 NSDate 预计 2010-07-12T09:00:00Z & 2010-07-12T18:00:00Z 形成计算的开始基础,同样适用于其间的天数。

是否需要在此基础上创建当年的数据集来计算结果?或者有没有一种聪明的方法可以让 NSCalendar / NSDateComponents 为我完成所有这些艰苦的工作?

I'm trying to figure out how to calculate store opening times based on the way they are normally expressed. For example: Monday - Friday, 9am - 6pm. I want to know two things from this criteria. Is the store open? If it is how long will it be before it closes?

My confusion with the documentation is that NSDate calculations expect unique date data sets to perform calculations not generic as expressed above. For example NSDate expects 2010-07-12T09:00:00Z & 2010-07-12T18:00:00Z to form the start basis of the calculation and like wise for the days in between.

Do I need to create a data set for the year on this basis to calculate the results? Or is there a clever way I can get NSCalendar / NSDateComponents to do all this hard work for me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁以何悠 2024-09-15 08:08:59

没有简单/自动的方法。您必须自己解析字符串,形成两个字符串,比如说 startDate 和 endDate (或类似的东西)。您可以从中提取星期几和时间(这就是 NSCalendar 和 NSDateComponents 派上用场的地方)。

There is no simple/automatic way. You have to parse the string yourself, form two, let's say startDate and endDate (or something alike). From them you can extract the day of the week and time (that's where NSCalendar and NSDateComponents come in handy).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文