获取正确的时间戳记45秒 [英] Get correct timestamp for 45 seconds ago

查看:177
本文介绍了获取正确的时间戳记45秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取有效的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆