如何在 Javascript 中将周数转换为日期
我曾试图找到一些已完成的工作,但我没有运气。有什么想法吗?
示例:
2001 年第 1 周 => 2001-01-01
2007年12月26日 => 2007-06-01
I had tried to find some work done but I haven't had luck. Any ideas?
Examples:
Week, 1, 2001 => 2001-01-01
Week, 26, 2007 => 2007-06-01
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于 Kevin 的代码没有正确实现 ISO 8601(一年第一周的第一天必须是星期一),我已更正它并最终得到(也 在 jsfiddle 上检查):
我包含了某些日期的测试用例,以检查一年中第一周的第一天是星期一,并根据 http://www.epochconverter.com/date-and-time/weeknumbers-by-year。 php
As Kevin's code does not implement ISO 8601 properly (first day of the first week of year must be a Monday), I've corrected it and ended up with (also check it on jsfiddle):
I included test cases for some dates to check that the first day of the first week of year is a Monday and checked some dates based on http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php
有人可能仍然对更包含的版本感兴趣:
Someone might be still interested in a more contained version:
我从 Kevin 那里得到了最初的想法,并进行了一些调整,因为原始代码返回毫秒。干得好:
I took the original idea from Kevin, with some tweaks, coz the original code is returning milliseconds. Here you go:
看看这个小提琴。首先,它获取指定年份的第一周。这考虑到,根据 ISO 8601,一年中的第一周是包含周三。然后它将周数与获取的日期相加并返回结果。
Take a look at this fiddle. First, it gets the first week of the specified year. This takes into account that according to ISO 8601 the first week of the year is the first week containing a wednesday. Then it adds the weeks to the acquired date and returns the result.