获取 45 秒前的正确时间戳 [英] Get correct timestamp for 45 seconds ago
问题描述
我正在尝试获取有效的 MongoDB 时间戳,我有这个:
I am trying to get a valid MongoDB Timestamp, I have this:
import {Timestamp} from "bson";
const ts = Timestamp.fromInt(Date.now() - 45000);
console.log(ts);
记录:
Timestamp { _bsontype: 'Timestamp', low_: 853265937, high_: 0 }
这似乎不对,我做错了什么?
that doesn't seem right, what am I doing wrong?
请注意,有效的时间戳实例是 64 位的:http://mongodb.github.io/node-mongodb-native/core/api/Timestamp.html
Note that a valid timestamp instance is a 64bit thing: http://mongodb.github.io/node-mongodb-native/core/api/Timestamp.html
推荐答案
要在 45 秒前获取时间戳:
To get Timestamp 45 seconds ago:
Timestamp((new Date().getTime()-45000)/1000,0)
Date().getTime() 给出来自 UNIX epoc 的毫秒数,所以这就是为什么它被 1000 除了.然后另一个参数 (0) 是在我们放置到第一个参数的秒值之后的零毫秒.
Date().getTime() gives milliseconds from UNIX epoc, so that's why it's divided with 1000. Then that other parameter (0) is just zero milliseconds after that seconds value what we put to the first parameter.
当然,如果您需要确切的 Timestamp() 值,您可以填充 (new Date().getTime()-45000)/1000
的小数部分作为第二个参数.
Of course, if you need exact Timestamp() value, you fill that decimal part of (new Date().getTime()-45000)/1000
as second parameter.
var x=(new Date().getTime()-45000)/1000;
var y=Math.floor(x);
var z=Math.round((x-y)*1000);
Timestamp(y,z)
这篇关于获取 45 秒前的正确时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!