16

例如“23:55:00”

我正在将巴士时刻表数据库从 Postgres 转换为 Mongo。
Postgres 有一个time without timezone我用来存储停止时间的数据类型,我很难弄清楚如何迁移它。

给它一个任意的年/月/日以将其转换为 javascript Date 对象似乎没有多大意义,但我确实计划在解析数据后对时间进行一些简单的计算,以显示类似'下一个停止时间',所以如果我将它存储为一个字符串,我仍然必须最终将它转换为一些东西来进行比较

我对 MongoDB 真的很陌生,任何建议都将不胜感激

4

1 回答 1

30

您可以将时间存储为数字(自 00:00:00 以来的秒数)。它将在需要时自然排序并轻松转换为 hh:mm:ss 格式。

//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
于 2013-04-03T09:07:51.573 回答