0

I need to store dates, the time is irrelevant(only need YYYY-MM-DD). I was wondering in which format should I store this. At the moment I store as a Date Object(ISO-8601) and if the date comes with some specific time I set that date to time 00:00:00(start of date).

This is what I do with the Moment.js

let dateToBeInserted = moment(date).startOf("date").toDate()

Or Should I simply store as string with the "YYYY-MM-DD" format or continue with this way?

Lucas Gomes
  • 316
  • 5
  • 16
  • 1
    You want to store the date where ?? Database , cache or a variable ???? I would rather use epoch time in long format to be stored in database. – Mukesh Verma Nov 02 '18 at 15:10
  • I will store in database. – Lucas Gomes Nov 02 '18 at 15:13
  • I have an apocryphal story where credit card expiration dates were being stored in some datetime format, but when moved between timezones resulted in a couple hours being subtracted from them. This resulted in a shift of losing a month (after truncating the "unimportant" time part), causing rejections. Moral of the story: either go full timezone-aware datetime, or store just the date. I prefer storing just the date. – roippi Nov 02 '18 at 15:14
  • Storing them as UTC timezone wouldn't solve that problem? Some people told me that you should store dates as UTC and then format that date to the client's machine timezone. But if I was so sure of it I wouldn't be asking this question. – Lucas Gomes Nov 02 '18 at 15:28
  • Typically a date-only value doesn’t have a time zone (e.g. birthday), they are just dates. Unfortunately ECMAScript treats ISO dates as UTC. If you want just the date, store it as just a date. If the Timezone matters (e.g. meetings) store date and time with timezone (commonly UTC adjusted to suit the user’s timezone). – RobG Nov 02 '18 at 22:54
  • 1
    Bottom line is that the question is inherently opinion based, but existing answers already correctly point out the primary difference is "storage size". A BSON Date takes considerably less space to store that most string formats, and this scales as longer strings with more precision take considerably more bytes to store per instance. A BSON Date is 8 bytes with millisecond precision whether you use it or not. Do whatever you want but the reasons for differences are already answered – Neil Lunn Nov 02 '18 at 23:19

0 Answers0