admin管理员组文章数量:1278886
How i can get the date of the first day of a week from the week number and the year, in javascript (or jquery)?
example : week 30 , year 2013
thanks in advance !
How i can get the date of the first day of a week from the week number and the year, in javascript (or jquery)?
example : week 30 , year 2013
thanks in advance !
Share Improve this question asked Jul 25, 2013 at 10:11 ThomasThomas 1,1403 gold badges18 silver badges37 bronze badges 5- 1 Possible duplicate of link – Chirag Vidani Commented Jul 25, 2013 at 10:17
-
You need to calculate that using Javascripts
Date
function. – putvande Commented Jul 25, 2013 at 10:17 - Duplicate to this too stackoverflow./questions/7580824/… – Chirag Vidani Commented Jul 25, 2013 at 10:18
- yes you're right chirag, it's a duplicated question. sorry :x – Thomas Commented Jul 25, 2013 at 12:38
- Possible duplicate of javascript calculate date from week number – Oleg Mikhailov Commented Jun 15, 2016 at 9:34
2 Answers
Reset to default 9var d = new Date(year, 0, 1);
var days = ['sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat'];
d.setDate(d.getDate() + (week * 7));
console.log(days[d.getDay()]);
Try This
Using ISO 8601 week numbering, you can start with the first week of the year and add (n - 1) * 7
days to get the first day of the required week. E.g.
function getDateByWeek(week, year) {
// Create a date for 1 Jan in required year
var d = new Date(year, 0);
// Get day of week number, sun = 0, mon = 1, etc.
var dayNum = d.getDay();
// Get days to add
var requiredDate = --week * 7;
// For ISO week numbering
// If 1 Jan is Friday to Sunday, go to next week
if (dayNum != 0 || dayNum > 4) {
requiredDate += 7;
}
// Add required number of days
d.setDate(1 - d.getDay() + ++requiredDate);
return d;
}
// Week 44 of 2021 starts on Mon 1 Nov 2021
console.log(getDateByWeek(44,2021).toString());
If some other week numbering scheme is required, just adjust the starting date accordingly.
本文标签: jqueryhow to get a javascript date from a week numberStack Overflow
版权声明:本文标题:jquery - how to get a javascript date from a week number? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741218835a2360570.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论