如何生成unix epoch格式的时间戳

Node.js - How to generate timestamp unix epoch format?

本文关键字:时间戳 格式 epoch 何生成 unix      更新时间:2023-09-26

我正在尝试使用

将数据发送到端口2003上的石墨碳缓存进程Ubuntu终端:

echo "test.average 4 `date +%s`" | nc -q0 127.0.0.1 2003

node . js:

var socket = net.createConnection(2003, "127.0.0.1", function() {
    socket.write("test.average "+assigned_tot+"'n");
    socket.end();
});

当我在ubuntu上使用终端窗口命令发送数据时,它工作得很好。但是,我不确定如何从nodejs发送时间戳unix纪元格式?

grpaite可以理解这种格式的metric metric_path value timestamp

谢谢!

原生JavaScript Date系统以毫秒为单位工作,而不是秒,但除此之外,它是相同的&;epoch time&;

您可以将一秒的小数取整,并通过以下操作获得UNIX纪元:

Math.floor(+new Date() / 1000)

Update:正如Guillermo指出的那样,另一种语法可能更具可读性:

Math.floor(new Date().getTime() / 1000)

第一个示例中的+是一个JavaScript怪现象,它强制将计算转换为数字,这与转换为毫秒具有相同的效果。第二个版本明确地做到了这一点。

如果可以,我强烈推荐使用moment.js。要获取自UNIX纪元以来的毫秒数,执行

moment().valueOf()

要获取自UNIX纪元以来的秒数,执行

moment().unix()

你也可以像这样转换时间:

moment('2015-07-12 14:59:23', 'YYYY-MM-DD HH:mm:ss').valueOf()

我一直这样做。

在Node上安装moment.js

npm install moment

和使用

var moment = require('moment');
moment().valueOf();

ref

在typescript中,只需运行Math.floor(new Date().getTime() / 1000)

> Math.floor(new Date().getTime() / 1000)
1581040613

我正在运行Node 13.7.0

简化它的辅助方法,复制/粘贴以下内容到你的JS之上:

Date.prototype.toUnixTime = function() { return this.getTime()/1000|0 };
Date.time = function() { return new Date().toUnixTime(); }

现在你可以通过简单的调用在任何你想要的地方使用它:

// Get the current unix time: 
console.log(Date.time())
// Parse a date and get it as Unix time
console.log(new Date('Mon, 25 Dec 2010 13:30:00 GMT').toUnixTime())
演示:

     
    Date.prototype.toUnixTime = function() { return this.getTime()/1000|0 };
    Date.time = function() { return new Date().toUnixTime(); }
    // Get the current unix time: 
    console.log("Current Time: " + Date.time())
    // Parse a date and get it as Unix time
    console.log("Custom Time (Mon, 25 Dec 2010 13:30:00 GMT): " + new Date('Mon, 25 Dec 2010 13:30:00 GMT').toUnixTime())

Post ECMAScript5可以使用:

Math.floor(Date.now() / 1000);

生成以秒为单位的时间戳

Using momentjs:

以下两个变量具有相同的值:-

console.log(moment().format('X'))
console.log(moment.utc().format('X'))

Using Date()内置:

console.log(new Date().getTime())   // including fractional seconds
console.log(Math.floor(new Date().getTime()/1000))    // up to seconds

AWS sdk包含用于转换amazon日期格式的实用函数。

例如,在S3 get对象的回调中,有一个属性'LastModified'是amazon日期格式的。(似乎他们什么都不做,但导出标准日期类为他们的日期属性,如S3对象'LastModified'属性)该格式包括一些用于各种内置格式的实用程序(不幸的是,unix epoch没有):

let awsTime = response.LastModified
console.log("Time Formats",{
    "String"           : awsTime.toString(),
    "JSON"             : awsTime.toJSON(),
    "UTCString"        : awsTime.toUTCString(),
    "TimeString"       : awsTime.toTimeString(),
    "DateString"       : awsTime.toDateString(),
    "ISOString"        : awsTime.toISOString(),
    "LocaleTimeString" : awsTime.toLocaleTimeString(),
    "LocaleDateString" : awsTime.toLocaleDateString(),
    "LocaleString"     : awsTime.toLocaleString()
})
/*
Time Formats {
  String: 'Fri Sep 27 2019 16:54:31 GMT-0400 (EDT)',
  JSON: '2019-09-27T20:54:31.000Z',
  UTCString: 'Fri, 27 Sep 2019 20:54:31 GMT',
  TimeString: '16:54:31 GMT-0400 (EDT)',
  DateString: 'Fri Sep 27 2019',
  ISOString: '2019-09-27T20:54:31.000Z',
  LocaleTimeString: '16:54:31',
  LocaleDateString: '2019-9-27',
  LocaleString: '2019-9-27 16:54:31'
}
*/

然而,AWS utils函数包含一个'date'模块,其他函数包括一个unixTimestamp方法:

let awsTime = response.LastModified
let unixEpoch = Math.floor(AWS.util.date.unixTimestamp(awsTime))

注意:这个方法默认返回一个浮点值。因此Math.floor()

函数代码来自于js -sdk.js(最新):

/**
 * @return [Integer] the UNIX timestamp value for the current time
 */
 unixTimestamp: function unixTimestamp(date) {
     if (date === undefined) { date = util.date.getDate(); }
     return date.getTime() / 1000;
 }

也有rfc822和iso8601的方法