将给定时间戳转换为 influxdb 时间戳
Convert given timestamp to influxdb timestamp
我的入职日期格式为:15.08.2017 23:03:23.120000
这里我使用 Node-Red 平台在 Influx 时间戳中转换 msg.payload.time
但我收到此错误:
"Error:
时间戳的预期数值,但得到的是“15.08.2017 23:03:23.120000'!"
.
请让我知道给定时间戳到 influxdb 时间戳的脚本。
InfluxDB expects unix timestamps 和 msg.payload.time
可能是一个字符串,因此您收到错误。
为了从日期生成时间戳,您可以使用 JS 的 Date
功能。
它以下列方式工作:
new Date('<your-date-string>').valueOf()
此处 date-string
应为 'YYYY-MM-DD hh:mm:ssZ' 格式。
对于您的情况,由于 msg.payload.time
以 dd.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())
我的入职日期格式为:15.08.2017 23:03:23.120000
这里我使用 Node-Red 平台在 Influx 时间戳中转换 msg.payload.time
但我收到此错误:
"Error:
时间戳的预期数值,但得到的是“15.08.2017 23:03:23.120000'!"
.
请让我知道给定时间戳到 influxdb 时间戳的脚本。
InfluxDB expects unix timestamps 和 msg.payload.time
可能是一个字符串,因此您收到错误。
为了从日期生成时间戳,您可以使用 JS 的 Date
功能。
它以下列方式工作:
new Date('<your-date-string>').valueOf()
此处 date-string
应为 'YYYY-MM-DD hh:mm:ssZ' 格式。
对于您的情况,由于 msg.payload.time
以 dd.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())