Nitesh
Nitesh

Reputation: 19

Convert given timestamp to influxdb timestamp

My Incoming Date is in format : 15.08.2017 23:03:23.120000

Here I am using Node-Red Platform to convert msg.payload.time in Influx timestamp but I am getting this Error:

"Error: Expected numeric value for, timestamp, but got '15.08.2017 23:03:23.120000'!".

Please let me know the script for given timestamp to influxdb timestamp.

Upvotes: 0

Views: 2163

Answers (1)

Dhruv Shah
Dhruv Shah

Reputation: 1661

InfluxDB expects unix timestamps and msg.payload.time might be a string, hence you are getting the error.

In order to generate a timeStamp from a date, you can use the Date functionality of JS. It works in the following way:

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

Here the date-string is expected in 'YYYY-MM-DD hh:mm:ssZ' format.

In your case, since the msg.payload.time is available in dd.mm.yy hh:mm:ssZ format, you will need to perform some additional operations.

You can update your code as below:

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();

Here is a working example

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())

Upvotes: 1

Related Questions