1

I am using this simple function to get YYYYMMDD format:

const yyyymmdd = (date) => {
    const day = date.getDate()
    const month = date.getMonth() + 1
    const year = date.getFullYear()

    console.log(date, day)

    let dayZero = (day < 10) ? "0" + day : day
    let monthZero = (month < 10) ? "0" + month : month

    return  year + "-" + monthZero + "-" + dayZero    
}

Note the console.log that prints the date and day.

The output for several calls of different dates are:

2020-10-18T06:36:31.116Z 18
2020-10-19T06:36:31.116Z 19
2020-10-20T06:36:31.116Z 20
2020-10-21T06:36:31.116Z 21
2020-10-22T06:36:31.116Z 22
2020-10-23T06:36:31.117Z 23
2020-10-24T06:36:31.117Z 24

2020-10-15T22:58:36.021Z 16
2020-10-15T22:58:36.021Z 16
2020-10-15T22:58:36.021Z 16
2020-10-15T22:58:36.021Z 16
2020-10-16T22:58:36.021Z 17

The last 5 results have inaccurate day for the format. The ones correct above are built with the following function and gives the expected results:

return new Date(new Date().getTime() + i * 24 * 60 * 60 * 1000)

But the ones that are wrong, are from MongoDB stored as a string and then converted to date as following:

new Date(mongo_db_string_date)

Can anyone help me understand why this happens? They seem to be the same date format, but may not be. How can I parse to get the right result?

J. Diaz
  • 371
  • 1
  • 6
  • 16
  • This sounds like a timezone issue. The Date functions use the zone information from your system when calculating day, month, etc. So you either need to make sure that all your calculations use UTC, or account for the timezone manually – pilchard Oct 17 '20 at 08:14
  • @JaromandaX this is the field stored in Mongo: ``` { start_date: '2020-10-15T22:58:36.021Z' } ``` – J. Diaz Oct 17 '20 at 08:16
  • This is a timezone issue. The dates are showing in UTC time and your local timezone is different. The last five indicate that your time is at least 2 hours ahead of UTC time - thus, 22:38 in UTC is either 00:38, 01:38, 02:38 etc in your timezone. The first ones will be ok as 06:36am in UTC time is still on the same date as in your timezone - so, could be 08:36, 09:36, 10:36 etc in your local time.. – ATD Oct 17 '20 at 08:17
  • @J.Diaz can you give an example of what value `mongo_db_string_date` is, and what the resultant date is - I mean, that's where you have the problem right? the `new Date(mongo_db_string_date)` is apparently wrong, so ... why not give an example – Jaromanda X Oct 17 '20 at 08:18
  • @JaromandaX but profiles can be misleading! He could be based in the UK but currently working in, say, Finland, which is 2 or 3 hours ahead of UK time. – ATD Oct 17 '20 at 08:19
  • @ATD - makes sense OP must be in +2 at least for that to happen – Jaromanda X Oct 17 '20 at 08:22
  • Guys, you are right, I am now very close to Finland, at UTC+2. – J. Diaz Oct 17 '20 at 08:22
  • @ATD ... how did you know OP was in finland!!! You're **amazing**!!!!! – Jaromanda X Oct 17 '20 at 08:23
  • Also, yet as you say, he hasn't provided an example, if the mongo date contains the "Z" suffix, it should still be UTC no matter where they are. Your sample date/time string, returns the same date/time for me even though we are currently on BST rather than GMT so is not being changed to 23:58. – ATD Oct 17 '20 at 08:23
  • @JaromandaX I have relatives in Finland - my sister-in-law is from just outside Helsinki – ATD Oct 17 '20 at 08:24
  • use `.getUTCDate()` etc? – Jaromanda X Oct 17 '20 at 08:25
  • @ATD, but of all the places UTC+2 to UTC+12 (since all those timezones would be effected the same way), you chose Finland :p – Jaromanda X Oct 17 '20 at 08:26
  • @JaromandaX Whilst that would remove any confusion and could add it as well - as it would make everything based on UTC, which may or may not be the desired result. – ATD Oct 17 '20 at 08:27
  • @JaromandaX If I didn't have relatives there, I probably would have picked somewhere like Moscow as a similar question related to there, and that is now 3 hours ahead of UTC. – ATD Oct 17 '20 at 08:29
  • Guys, `.getUTCDate()` solved the issue, I will also use `.getUTCMonth()` etc. Many thanks from Estonia :D – J. Diaz Oct 17 '20 at 08:36
  • @JaromandaX Do you want to put that into an Answer, so that the OP can flag that as correct? – ATD Oct 17 '20 at 08:39
  • @ATD - not really - the question is, does the OP want UTC day or local day - perhaps these comments will make OP think more about what output is required – Jaromanda X Oct 17 '20 at 08:41
  • @JaromandaX I was thinking that as well. If a file, say, was created at a particular time in one country, would we think of it as being created at the same time in our country or would we allow for timezoning and think of it being created at a different date/time in our country as that would be the exact same moment? – ATD Oct 17 '20 at 08:45
  • You should also see [*How can I add 1 day to current date?*](https://stackoverflow.com/questions/9989382/how-can-i-add-1-day-to-current-date) – RobG Oct 17 '20 at 21:19

1 Answers1

0

The date you're generating is in the same timezone. Pretty sure the mongo_db_string_date has its own timezone info and causing this difference. Sorry, this should be a comment but I don't have enough points for it.

HaiTH
  • 925
  • 9
  • 9