1

I'm making a small employee management system in which I have to play with dates in many places like attendance, leaves etc.

I use luxon for dates in my node.js project. Ex: DateTime.fromISO("2020-10-15"); gives me this in the console: 2020-10-15T00:00:00.000+05:00

but when I save it in mongodb it becomes 2020-10-14T19:00:00.000+00:00 and this is problematic because i need to query objects to check if there is an attendance marked in the same day or to get today's attendance. How can I solve this issue?

  • Please refer to the following post, it may help you with the dates on mongodb: https://stackoverflow.com/questions/3778428/best-way-to-store-date-time-in-mongodb – Gi1ber7 Oct 15 '20 at 01:05

2 Answers2

0

MongoDB only has a timestamp type, it does not have a date type. Dates are converted to times when they are stored. Which timezone the date is interpreted to be in can vary, looks like your driver uses the beginning of specified date in UTC. Ruby works the same way and you can read more about the principle here.

To "solve this issue", either only store and query by timestamps in MongoDB, converting dates to timestamps in your application, or understand how your language and driver converts dates to timestamps (note that JS Date type is actually a timestamp).

D. SM
  • 13,584
  • 3
  • 12
  • 21
0
  1. Storage

    When MongoDB stores time-type data, it is first converted to UTC time and then stored in the database. The date submitted by the client is East 5, and the conversion to UTC time is minus 5 hours;

  2. Read

    MongoDB will convert the UTC date stored in the database into the East 5 zone of the client according to the time zone of the client, and then return it to the client

Mario Codes
  • 689
  • 8
  • 15