Search code examples
javascriptmongodbmeteormeteor-autoform

Save time of day as number in mongodb, but display in a human format (using meteor autoform)


In a form, I'm asking for a start time & end time, which I'll be using in a script later on. I figured in MongoDB, a time of day is best stored as the # of seconds since midnight (per How can I store time-of-day in MongoDB? As a string? Give arbitrary year/month/day?).

My question is: how can I display a human-readable time in autoform (e.g. 7:30pm) yet still save it as a number in mongodb & have proper client-side validation (make sure time is before 8:00pm)? I figure I could either use a datetime object & subtract the seconds since 1970, or I could parse the time string & do math on the hours, minutes, AM/PM.

Thoughts on methods? & where to put the math hooks in autoform? This seems like something folks a lot smarter than me have probably already figured out!


Solution

  • new Date(<unixTimeStamp>) will get you a javascript Date object which can be easily played with using libraries like moment (https://github.com/moment/moment)

    Edit: also, to get the proper timestamp you can do +Date.now(). This jives with mongodb's date type