Best way to store time of day in Mongoose

前端 未结 1 1746
[愿得一人]
[愿得一人] 2021-01-14 19:19

I\'m creating a schema in Mongoose, and I\'m trying to figure out the best way to store a field representing the time of day ie 3:30, it does not need to be a fully qualifie

相关标签:
1条回答
  • 2021-01-14 19:43

    I'd suggest storing it either as seconds since midnight (as a Number) or as a padded numeric String stored in 24 hour format.

    For example, 3:30PM:

    1. Seconds (stored as a number): 55800
    2. String: "1530" (always must be 24 hour format with a leading numeric digit to have the same number of places, so 8:30AM would be "0830"

    Both can be sorted, indexed, queried by range. Both take approximately the same number of bytes. Since neither is very human friendly readable, you'd probably need to format them either way for display. It's really up to you which one would work better for your use.

    0 讨论(0)
提交回复
热议问题