InfluxDB requires unix timestamps and msg.payload.time
could be in string format, leading to the error you encountered.
To convert a date into a timeStamp, you can utilize JavaScript's Date
functionality as follows:
new Date('<your-date-string>').valueOf()
The date-string
should follow the 'YYYY-MM-DD hh:mm:ssZ' format.
In this scenario, where the msg.payload.time
is in dd.mm.yy hh:mm:ssZ
format, additional steps are necessary.
You can adjust your code like so:
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 separately 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();
Below is a functional demonstration:
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())