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

前端 未结 1 1194
盖世英雄少女心
盖世英雄少女心 2020-12-13 15:48

e.g. \"23:55:00\"

I\'m in the process of converting a bus schedule database from Postgres to Mongo.
Postgres has a time without timezone datatype th

相关标签:
1条回答
  • 2020-12-13 16:12

    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 time
    storedTime = hours * 3600 + minutes * 60 + seconds
    
    //from the stored time to 23:55:00
    hours = storedTime / 3600  // needs to be an integer division
    leaves = storedTime - hours * 3600
    minutes = leaves / 60
    seconds = leaves - 60 * minutes
    
    0 讨论(0)
提交回复
热议问题