问题
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);
that logs:
Timestamp { _bsontype: 'Timestamp', low_: 853265937, high_: 0 }
that doesn't seem right, what am I doing wrong?
Note that a valid timestamp instance is a 64bit thing: http://mongodb.github.io/node-mongodb-native/core/api/Timestamp.html
回答1:
To get Timestamp 45 seconds ago:
Timestamp((new Date().getTime()-45000)/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.
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)
来源:https://stackoverflow.com/questions/49333616/get-correct-timestamp-for-45-seconds-ago