admin管理员组文章数量:1195956
In JavaScript I want to get first date of the week and last date of the week by week number and year only.
For example if I my input is:
2(week),2012
Then my output should be:
2012-01-08 and 2012-01-14
In JavaScript I want to get first date of the week and last date of the week by week number and year only.
For example if I my input is:
2(week),2012
Then my output should be:
2012-01-08 and 2012-01-14
- How about if I give 2 for year 2013? What should it return? – James Jithin Commented Jan 10, 2012 at 12:27
- it should return the second week of the first date and last date.... – balaphp Commented Jan 10, 2012 at 12:29
- @balaphp actually it will return first and last date of 2nd week of the year 2013..m i right. ? – Chandresh M Commented Jan 10, 2012 at 12:32
6 Answers
Reset to default 9Try this:
var year = 2012;
var week = 2;
var d = new Date("Jan 01, " + year + " 01:00:00");
var w = d.getTime() + 604800000 * (week - 1);
var n1 = new Date(w);
var n2 = new Date(w + 518400000)
console.log(n1);
console.log(n2);
n1 contains the first day of the week
n2 contains the last day of the week
As for the constants:
604800000 is one week in milliseconds
518400000 is six days
A little change to @bardiir 's answer, if the first day of the year is not Sunday(or Monday) that result is not correct. You should minus the number of the first day.
Changed code
firstDay = new Date(2015, 0, 1).getDay();
console.log(firstDay);
var year = 2015;
var week = 67;
var d = new Date("Jan 01, " + year + " 01:00:00");
var w = d.getTime() - (3600000 * 24 * (firstDay - 1)) + 604800000 * (week - 1)
var n1 = new Date(w);
var n2 = new Date(w + 518400000)
console.log(n1.toString());
console.log(n2.toString());
if you wish the first is Sunday, change (firstDay-1)
to firstDay
you can check and try with below link.
How to get first and last day of the week in JavaScript
It will may helpful to you.
Thanks.
Change previous answers and make it clear. @bardiir
// give the year and week
let year = 2021;
let week = 1;
// first date of year
let firstDateOfYear = new Date(year, 0, 1);
// get the day of first date in the year
let firstDayOfYear = firstDateOfYear.getDay();
console.log("first day of year", firstDayOfYear);
let timeofOneDay = 60 * 60 * 24 * 1000;
let timeofOneWeek = 60 * 60 * 24 * 7 * 1000;
// last day of the week, 6 days later
let timeof6Day = 60 * 60 * 24 * 6 * 1000;
// if week start from Monday
let timeOfFirstDay = firstDateOfYear.getTime() - (timeofOneDay * (firstDayOfYear - 1)) + timeofOneWeek * (week - 1);
let timeOfLastDay = timeOfFirstDay + timeof6Day;
console.log("week start from Monday");
console.log(new Date(timeOfFirstDay).toString());
console.log(new Date(timeOfLastDay).toString());
// if week from Sunday
timeOfFirstDay = firstDateOfYear.getTime() - (timeofOneDay * (firstDayOfYear - 0)) + timeofOneWeek * (week - 1);
timeOfLastDay = timeOfFirstDay + timeof6Day;
console.log("week start from Sunday");
console.log(new Date(timeOfFirstDay).toString());
console.log(new Date(timeOfLastDay).toString());
dt = new Date();
var firstDateOfWeek=(dt.setDate(dt.getDate()-dt.getDay()));
var lastDateOfWeek=(dt.setDate(dt.getDate()+6-dt.getDay()));
The powerful way is for this issue, is to use moment.js
let date=new Date();
let start_date = moment(date).startOf('date').toDate()
let end_date = moment(date).endOf('date').toDate()
本文标签: javascriptHow to get first date and last date of the week from week number and yearStack Overflow
版权声明:本文标题:javascript - How to get first date and last date of the week from week number and year? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738532113a2093810.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论