10

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

M.A.K. Ripon
  • 2,070
  • 3
  • 29
  • 47
balaphp
  • 1,306
  • 5
  • 20
  • 40

6 Answers6

9

Try 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

M.A.K. Ripon
  • 2,070
  • 3
  • 29
  • 47
bardiir
  • 14,556
  • 9
  • 41
  • 66
  • 4
    Incorrect, n1 is not necessarily the monday and n2 is not necessarily the sunday. – Mark Buikema Sep 11 '14 at 21:25
  • `new Date("Jan 01, " + year + " 01:00:00")` is a poor way of creating a Date for 1 January as it relies on built–in parsers with an unsupported string format. Setting the hour to 1 seems unnecessary. `new Date(year, 0)` is shorter and much preferred. Milliseconds in 1 day, week, whatever aren't consistent where daylight saving is observed. – RobG Dec 02 '21 at 20:46
6

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

RobG
  • 142,382
  • 31
  • 172
  • 209
Xig480
  • 69
  • 1
  • 2
  • `new Date("Jan 01, " + year + " 01:00:00")` is not sensible as it relies on the built–in parser and an unsupported format. `new Date(year, 0)` is equivalent, reliable and less to type. Not all days are 24 hours long where daylight saving is observed, so results may be wrong occasionally. This answer also assumes week numbering starts from 1 Jan, which in many cases it doesn't, and returns weeks like Tue 5 Apr 2022 to Mon 11 Apr 2022. – RobG Dec 03 '21 at 00:46
1

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.

Community
  • 1
  • 1
Chandresh M
  • 3,808
  • 1
  • 24
  • 48
  • i had checked it already.. its returning values by date but i want same results by week number and year... – balaphp Jan 10 '12 at 12:34
0

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());
Jian Zhong
  • 451
  • 3
  • 10
-1
   dt = new Date();
   var firstDateOfWeek=(dt.setDate(dt.getDate()-dt.getDay()));
   var lastDateOfWeek=(dt.setDate(dt.getDate()+6-dt.getDay()));
-1

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()
dragon99k
  • 11
  • 6
  • Momentjs is [depreciated](https://momentjs.com/docs/) and should use an alternative library such as [date-fns](https://date-fns.org/). – Tyler2P Aug 10 '22 at 17:35