【问题标题】:Convert given timestamp to influxdb timestamp将给定的时间戳转换为 influxdb 时间戳
【发布时间】:2020-07-08 12:29:08
【问题描述】:

我的接收日期格式为:15.08.2017 23:03:23.120000

我在这里使用 Node-Red 平台将 msg.payload.time 转换为 Influx 时间戳,但我收到此错误:

"Error: 应为时间戳的数值,但得到的是 '15.08.2017 23:03:23.120000'!"

请告诉我给定时间戳到 influxdb 时间戳的脚本。

【问题讨论】:

  • 你把COIL_ID放在fieldstags中,这是故意的吗?

标签: javascript arrays json influxdb node-red


【解决方案1】:

InfluxDB expects unix timestampsmsg.payload.time 可能是一个字符串,因此您会收到错误消息。

为了从日期生成时间戳,您可以使用 JS 的Date 功能。 它的工作方式如下:

new Date('<your-date-string>').valueOf()

此处date-string 应为“YYYY-MM-DD hh:mm:ssZ”格式。

在您的情况下,由于msg.payload.timedd.mm.yy hh:mm:ssZ 格式提供,您将需要执行一些额外的操作。

您可以按如下方式更新您的代码:

const incomingDate = msg.payload.time;

// extract the date dd.mm.yyyy from the incoming Date String
const splittedDate = incomingDate.split(' ');

// Convert the date from dd.mm.yyyy to yyyy-mm-dd format
let date = splittedDate[0].split('.').reverse().join('-');

// Store time value in a separate variable for later use.
const time = splittedDate[1];

// merge date and time to form yyyy-mm-dd hh:mm:ssZ format
const datetime = `${date} ${time}`

// assign the timestamp value to fields.time
fields.time =  new Date(datetime).valueOf();

这是一个工作示例

const incomingDate = '15.08.2017 23:03:23.120000';
const splittedDate = incomingDate.split(' ');
let date = splittedDate[0].split('.').reverse().join('-');
const time = splittedDate[1];
const datetime = `${date} ${time}`
console.log(datetime);
console.log(new Date(datetime).valueOf())

【讨论】:

  • 感谢您的回答