How can I store time-of-day in MongoDB? As a string? Give arbitrary year/month/day? How can I store time-of-day in MongoDB? As a string? Give arbitrary year/month/day? mongodb mongodb

How can I store time-of-day in MongoDB? As a string? Give arbitrary year/month/day?


You could store the time as a number (number of seconds since 00:00:00). It will be naturally sorted and easy to convert from/to hh:mm:ss format when needed.

//from 23:55:00 to the stored timestoredTime = hours * 3600 + minutes * 60 + seconds//from the stored time to 23:55:00hours = storedTime / 3600  // needs to be an integer divisionleaves = storedTime - hours * 3600minutes = leaves / 60seconds = leaves - 60 * minutes