Get correct timestamp for 45 seconds ago

只谈情不闲聊 提交于 2019-12-24 10:49:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!