1

I need to calculate the date of the last Monday that before the latest weekend. Having used what I believe to be the most common Stack Overflow suggestions I have the following code:

const getDayOfTheWeek = () => {
  let date = new Date();
  let clonedDate = new Date(date.getTime());
  console.log(clonedDate);
  const dow = clonedDate.getDay();
  console.log(dow);
  const offset = dow+6;
  console.log(offset);
  const newDate = new Date(clonedDate.setDate(clonedDate.getDate() - offset));
    console.log(newDate);
  const newNewDate = new Date(newDate.getTime());
  console.log(newNewDate);
  const day = newNewDate.getDate();
  const month = newNewDate.getMonth();
  const year = newNewDate.getYear();

  console.log('the year is ',year, 'the month is ', month);
}
getDayOfTheWeek();

It returns the year as 118 and the month as 5 which are ... not that Monday I need. newNewDate, on the other hand is the last Monday. I was wondering what causes it. I am aware that there are too many reassignments that are not needed. Please help.

Katia Punter
  • 308
  • 3
  • 13
  • Why do you need to copy the date at all? You are not using `date` after creating `clonedDate`, neither are you using `clonedDate` after creating `newDate`, neither are you using `newDate` after creating `newNewDate`. But anyways, calling `new Date` always creates a new date object, so sure, that would "clone" the date (using the term "deep" cloning doesn't make sense since a Date does not contain other values). – Felix Kling Jul 04 '18 at 18:24
  • It seemed that it might explain year 118. – Katia Punter Jul 04 '18 at 18:28
  • Not at all: `new Date().getYear() // 118`. That's how `getYear` works: *"For years greater than or equal to 2000, the value returned by getYear() is 100 or greater. For example, if the year is 2026, getYear() returns 126."* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getYear – Felix Kling Jul 04 '18 at 18:31
  • You should use `getFullYear()` instead. – Felix Kling Jul 04 '18 at 18:31
  • And the month being 5? – Katia Punter Jul 04 '18 at 18:32
  • *"The `getMonth()` method returns the month in the specified date according to local time, **as a zero-based value** (where zero indicates the first month of the year)."* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getMonth . `5` => sixth month of the year => June – Felix Kling Jul 04 '18 at 18:33
  • You could have verified your hypothesis about the cloning being the issues by running `new Date().getMonth()` in your browser's console. – Felix Kling Jul 04 '18 at 18:35
  • And now that you updated the title, it's a duplicate of https://stackoverflow.com/q/98124/218196 . – Felix Kling Jul 04 '18 at 18:36

1 Answers1

3

Whatever you are doing is perfect only, the only mistake is you are using getMonth() and getYear() and misunderstanding them.

date.getMonth() gives you months ranging from 0-11. So 5 is actually June month.

date.getYear() this method returns the year minus 1900, so actual is 118+1900=2018, instead you can use date.getFullYear() which will return 2018

Also, you don't need so many steps.

the function can be simply stopped with newDate as given below

const getDayOfTheWeek = () => {
  let date = new Date();
  let clonedDate = new Date(date.getTime());
  console.log(clonedDate);
  const dow = clonedDate.getDay();
  console.log(dow);
  const offset = dow+6;
  console.log(offset);
  const newDate = new Date(clonedDate.setDate(clonedDate.getDate() - offset));
    console.log(newDate);
  const day = newDate.getDate();
  const month = newDate.getMonth() + 1;
  const year = newDate.getFullYear();

  console.log('the year is ',year, 'the month is ', month);
}
getDayOfTheWeek();

This will give "the year is 2018 the month is 6"

Hope this helps.

Nilesh Soni
  • 405
  • 5
  • 12
  • 1
    `clonedDate` and `newDate` are not needed either. – Felix Kling Jul 04 '18 at 18:32
  • @FelixKling I think clonedDate will be needed in case of safe side, I don't know what minimum tick does JS uses, but if called Date() constructor again to create a new date, and suppose that time is when date is changing, I mean what if t1 = new Date() is today & t2 = new Date() the very next statement is tomorrow. – Nilesh Soni Jul 04 '18 at 18:36
  • The problem was as you pointed out NOT the deep copying but me not checking what getYear() returns! I was too confident I used it before and it worked fine. Thank you all for your help. – Katia Punter Jul 04 '18 at 18:39
  • 1
    I'm saying there is no need to create a second (or third) date object. Everything can be done with `date`. https://jsfiddle.net/m1py98vx/1/ – Felix Kling Jul 04 '18 at 18:51
  • @FelixKling yeah right, I only concentrated on one part, so skipped optimization. that fiddle is perfect. – Nilesh Soni Jul 04 '18 at 18:59