0

When I write a date in my input I receive the wrong time in the database (2 hours earlier than the time I want), in the pictures you can see that I got the hour 5:21 in the database even though I wrote the time 7:21 in the input:

The time in the input: the time in the input

The time in the database:

the time in the database

I thought that it is maybe because of the time zone (I am In Israel) and I try to use the Library Moment.

In this way:

holiday.beginDate = moment.tz(req.body.beginDate, "Asia/Jerusalem");

But, I get the same result.

Someone can tell me what I am doing wrong or what should I do to make the time I write in the input be the same time in the database?

Thank you.

fatihyildizhan
  • 8,614
  • 7
  • 64
  • 88
  • 3
    MongoDB stores all date-times in UTC. That would be 2 hours behind your local time, rigth? – Joe Apr 24 '21 at 08:14
  • Yes, in Israel winter clock is 2 hours differences and in Israel summer clock is 3 hours differences from UTC – Gilad Dekel Apr 24 '21 at 08:39
  • 2
    So if 7:21 is in Israel Time (IST), then the time stored is correct. – Ole V.V. Apr 24 '21 at 09:06
  • but I need the time to be 7:21 in the database, and it is written as 5:21 there, – Gilad Dekel Apr 24 '21 at 09:13
  • Try `holiday.beginDate = moment.tz(req.body.beginDate, "Asia/Jerusalem").toDate()` and you may need to specify the input format. – Wernfried Domscheit Apr 24 '21 at 09:24
  • You are telling me that you need to have the wrong time in the database? I sure hope that you’re mistaken there. – Ole V.V. Apr 24 '21 at 18:00
  • Please see [Any way to save Date type as local time in MongoDB?](https://stackoverflow.com/questions/66445434/any-way-to-save-date-type-as-local-time-in-mongodb) – Joe Apr 25 '21 at 08:49
  • 1
    How can I display the time on the client-side as my local time and not UTC? thank you – Gilad Dekel Apr 25 '21 at 09:18

0 Answers0