0

I seem to be having a similar problem to this thread, where my JS dates are 1 day off, but I'm struggling a little to use the answers for a solution.

My problem seems to be down to timezone differences, BST(+1) vs GMT

For example, the clocks go forward two days after the first (correct) example, and then the rest are off:

console.log(new Date('2022', '02', '24')); // 24 (2022-03-24T00:00:00.000Z)
console.log(new Date('2022', '09', '24')); // 23 (2022-10-23T23:00:00.000Z)
console.log(new Date('2022', '08', '24')); // 23 (2022-09-23T23:00:00.000Z)

From the docs and that thread: If I create a date using strings it'll be in UTC, which is an hour behind me currently. And so because I'm not providing a time it assumes 00:00, which is actually 23:00UTC the previous day, right?

So I should use getTimezoneOffset() if I want to get a consistent date 7 days ago?:

const oneWeekAgo = new Date(
    utcDate.getFullYear(), 
    utcDate.getMonth(), 
    utcDate.getDate() - 7
) + utcDate.getTimezoneOffset();

This seems to get the date right, but not the format.

I just wanted to double check my approach so far, because the answer in the other thread seems to give the reasoning as being west of or behind UTC, when I'm currently east or infront, and the two didn't seem compatible as answers

Given a date string of "March 7, 2014", [Date.]parse() assumes a local time zone, but given an ISO format such as "2014-03-07" it will assume a time zone of UTC.

NickW
  • 1,207
  • 1
  • 12
  • 52
  • What's the "right" format? – kelsny Oct 24 '22 at 18:34
  • What exactly is the problem? `new Date('2022', '09', '24')` will give you the 24th of September 2022 *local time*. If you do `getDate()` you get `24`. Why exactly does it matter what the UTC date is? And if you *do* use the UTC date, chances are that's correct, as it's agnostic to local time. – VLAZ Oct 24 '22 at 18:37
  • `new Date('2022', '09', '24')` gives me `2022-10-23T23:00:00.000Z` – NickW Oct 24 '22 at 18:40
  • Hi @caTS, sorry, I should've said, this is what I'm after (don't really care about the time): `2022-10-23T23:00:00.000Z` – NickW Oct 24 '22 at 18:41
  • Yes, that's the date in UTC. But when you interrogate the date object itself, you get local date information. Unless you use the `getUTC...` methods. – VLAZ Oct 24 '22 at 18:42
  • 1
    When it's midnight on Oct 24 in your time zone, it's 11pm on the 23rd in UTC. Expected. Why do you care about UTC time? – James Oct 24 '22 at 18:43
  • I'm trying desperately not to care about UTC but it won't leave me alone :) How can I consistently get the date 7 days ago then? This was my previous method `const weekStartDate = new Date(today.getFullYear(), today.getMonth(), today.getDate() -7)`, but that gets the 16th, not the 17th – NickW Oct 24 '22 at 18:47
  • That's [a different question](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) than "off by one (Timezones)". Also, the answer is not really exciting: `date = new Date("2022-03-30"); date.setDate(date.getDate() - 7); console.log(date.getDate())`. The result is `23`, as expected. Even though the clocks moved an hour forward on 27th of March. Similarly `date = new Date("2022-03-23"); date.setDate(date.getDate() + 7); console.log(date.getDate())` results in `30`. – VLAZ Oct 24 '22 at 18:59
  • When I test your above weekStartDate code after changing my timezone I get `Mon Oct 17 2022 00:00:00 GMT+0100 (British Summer Time)` check https://jsfiddle.net/923u5bdy/ – James Oct 24 '22 at 19:02
  • @James your console shows it in local format. OP's shows the timestamp in UTC. "Mon Oct 17 2022 00:00:00 GMT+0100 (British Summer Time)" is "2022-10-16T23:00:00Z" in ISO 8601 format in UTC timezone. Or "2022-10-17T00:00:00+01:00" in ISO 8601 in your local time zone. – VLAZ Oct 24 '22 at 19:05
  • Thanks @VLAZ. I'd got the solution to the problem from here (https://bobbyhadz.com/blog/javascript-get-last-week-date#get-last-weeks-date-using-javascript), which apparently was wrong. Anyway, so is there a general rule here? When manipulating dates always use setters/getters? Thanks again, appreciate it – NickW Oct 24 '22 at 19:43
  • 1
    Yeah. Manipulate the date object and use the date object. That's the general rule. It does not work 100% of the time but it's definitely a start. Re-creating dates is not great. Unless you want to just do a clone with `new Date(oldDateObject)`. Using a date library is also a good idea for anything more complex. – VLAZ Oct 24 '22 at 19:45
  • Yeah, that's definitely a lesson it's taught me for future projects. Thanks – NickW Oct 24 '22 at 19:53

0 Answers0