1

I am using Angular 10.

I have a function to parse the date:

convertDateToDb(dateStr) {
    let dateStrInput = null;
    
    var parts = dateStr.trim().replace(/ +(?= )/g,'').split(/[\s-\/:]/)
    if (parts[0].length === 4) {
      //YYYY-MM-DD OR YYYY/MM/DD format
      dateStrInput = parts[0] + '/' + parts[1] + '/' + parts[2]  + ' 00:00';
    } else if (parts[0].length === 2) {
      //MM/DD/YYYY or MM-DD-YYYY
      dateStrInput = parts[2] + '/' + parts[0] + '/' + parts[1]  + ' 00:00';
    } 
    let valDate = Date.parse(dateStrInput);
    console.log("Parsed Date ms: " + valDate.toString());
    console.log("Parsed Date: " + new Date(valDate).toString());
    // The above prints with timezone BST, but correct value
    // Tue Jun 30 2020 00:00:00 GMT+0100 (British Summer Time)
    if (!valDate) {
      return new Date().toISOString().split('T')[0];
    }
    // Now when I use new Date() with string - "06/30/2020 00:00" or "2020-06-30 00:00"
    // This gives output "2020-06-29" if any London /Asia user runs.
    return new Date(dateStrInput).toISOString().split('T')[0];

  }

When I pass "2020-06-30" or "06/30/2020", it returns correct value (2020-06-30). However, when users in other timezone (London / Asia) passes the same value, the date is returned as 2021-06-29 (previous day).

I am unable to re-produce on Windows 7 by changing my machine's timezone.

  1. Is there a scratch pad or playground online, where I can change the timezone to test my code?
  2. What is the change in code I can do to ignore timezone, just read the string value passed and convert it. (The issue is with new Date function, how should I tell him not to use timezone)?

I cleared all the cache of browser and changed my machine timezone to London. I am able to replicate this issue.

Now, after changing the timezone, I went to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset

In its editor I am trying to test:

ddtt = new Date("Tue Jun 30 2020").toISOString().split('T')[0];
console.log(ddtt);

Simple string, no TZ or anything.

The output is: enter image description here

How should I use new Date to ignore timezone and give date based on string passed?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Mihir
  • 531
  • 2
  • 10
  • 35
  • Why do you feel you need to remove timezone? Why not let the browser handle different times automatically for you? 9:00 AM PDT is the same as 10:00 AM MDT right? If you remove timezones how then do you handle time for users in different locations? If the event is stored to occur on 9:00 AM PDT, you wouldn't show someone in MDT that it occurs at 9:00 AM MDT right? As long as you stored the data as UTC/ISO in the database, the browser handles the conversions for you without any additional effort in line with the users location. – Alexander Staroselsky Aug 06 '21 at 16:20
  • For example, what date and time do you expect the following string `Fri Aug 06 2021 10:25:13 GMT-0600 (Mountain Daylight Time)` to be in YOUR time zone? As in day month hour and minute. Let's its the time I scheduled an important meeting for that I invited you to that you need to attend. Hint, you can do `new Date("Fri Aug 06 2021 10:25:13 GMT-0600 (Mountain Daylight Time)")` in your browser console. – Alexander Staroselsky Aug 06 '21 at 16:29
  • Here the problem is not keeping or removing timezone. It is to parse the date correctly. If the value input is 06/30/2020 or 2020-06-30 or 2020/06/30 or 06-30-2020, the parsed value should be "2020-06-30". In this case it is changing to "2020-06-29" for users from London/Asia. So, I am looking to fix that. In this function I am not using timezone or time to store anything. – Mihir Aug 06 '21 at 16:59
  • Does this answer your question? [Why does Date.parse give incorrect results?](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – Heretic Monkey Aug 06 '21 at 18:51

0 Answers0