使用 NSDate 根据一周中的通用天数计算时间
我试图弄清楚如何根据通常的表达方式来计算商店开门时间。例如:周一至周五,上午 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有简单/自动的方法。您必须自己解析字符串,形成两个字符串,比如说 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).