Convert given timestamp to influxdb timestamp - javascript

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.

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

Related

Use date-fns to format day as UTC

I have the following date format 2022-07-20 and I would like to parse into the following format 2022-07-20T00:00:00.000Z
I'm using the following code
const main = () => {
const day = '2022-07-20'
const date = new Date()
const result = parse(day.toString(), 'yyyy-MM-dd', date)
console.log(result.toISOString())
}
And getting the following output 2022-07-19T21:00:00.000Z. I assume this happening because my timezone is UTC+3.
How do I get it formatted as 2022-07-20T00:00:00.000Z?

Formatted date as String converted to date wrong

I am trying to store date in DB using the current timezone.
I have used library date-fns-tz When I convert the date as a string is okay, but when I want to create that string as a Date it is still a problem.
const date = new Date();
const date_string = formatInTimeZone(
date,
"Europe/Rome",
"yyyy-MM-dd HH:mm:ss"
);
console.log(date_string); //2022-04-16 11:46:28 (Right time)
const current_date = new Date(date_string); // 2022-04-16T09:46:28.000Z (Converted to 2h back)

ERROR Error: InvalidPipeArgument: "12-01-2020 - 12-02-2020' to a date' for pipe 'DatePipe'

I am trying to concatenate two dates and assigning to variable but it is throwing an error
ERROR Error: InvalidPipeArgument: 'Unable to convert “12-01-2020 - 13-02-2020” into a date' for pipe 'DatePipe'.where I am going wrong?
dates which I am getting from backend is projectStartDate: "2020-12-21T13:55:00.000+00:00".I am converting it to 12-01-2020 / timestamp after that concatenating both and assigning to projectduration value.
this.startDate = this.datepipe.transform(response.projectStartDate, 'yyyy-MM-dd','es-ES');
this.endDate = this.datepipe.transform(response.projectEndDate, 'yyyy-MM-dd','es-ES');
response.gbRFEbean.projectDuration.value = this.startDate + "-" +this.endDate ;
The Date format: DD-MM-YYYY is invalid.
You can try using :
new Date("12-01-2020")
It will give Invalid Date in chrome dev tools.
Solution
You can change the date format to MM-DD-YYYY and then pass it to datepipe.transform
let date = response.projectStartDate.replace(/(\d{2})-(\d{2})-(\d{4})/, "$2-$1-$3")
this.startDate = this.datepipe.transform(date, 'yyyy-MM-dd','es-ES');
date = response.projectEndDate.replace(/(\d{2})-(\d{2})-(\d{4})/, "$2-$1-$3")
this.endDate = this.datepipe.transform(date , 'yyyy-MM-dd','es-ES');
locale is the fourth argument of the DatePipe transform and timezone is the third argument. Currently you have the locale as the third argument. I'd probably configure your locale like this (and the date pipe will automatically work for your locale without specifying):
Missing locale data for the locale "XXX" with angular
transform takes either a javascript date or an ISO date string so I'm guessing your dates (projectStartDate, projectEndDate) are strings that aren't in the ISO format? The error message implies that one of those fields has the entire value of 12-01-2020 - 13-02-2020
sample code with ISO date string
startDate = '2020-01-12';
endDate = '2020-02-13';
formattedStartDate = this.datePipe.transform(this.startDate);
formattedEndDate = this.datePipe.transform(this.endDate);
constructor(private datePipe: DatePipe) {
console.log(`${this.formattedStartDate} - ${this.formattedEndDate}`);
}
Sample code with javascript date
startDate = new Date('2020-01-12');
endDate = new Date('2020-02-13');
formattedStartDate = this.datePipe.transform(this.startDate, 'yyyy-MM-dd');
formattedEndDate = this.datePipe.transform(this.endDate, 'yyyy-MM-dd');
constructor(private datePipe: DatePipe) {
console.log(`${this.formattedStartDate} - ${this.formattedEndDate}`);
}
If your dates are strings in a dd-MM-yyyy format, you can parse them with a library like luxon:
npm i luxon
And import
import { DateTime } from 'luxon';
And parse
startDate = DateTime.fromFormat("12-01-2020", "dd-MM-yyyy").toISODate();
As a final note it's a little funny to put the dates in an ISO date format (yyyy-MM-dd) and then format them in the same format. The pipe makes more sense, particularly with you app locale set, if you want to use formats such as mediumDate (the default) which might display 12 ene. 2020

How to convert dates to UNIX timestamps in MomentJS?

I want to convert my date time values to Unix timestamp format (basically an epoch timestamp). For that I use:
let startDate = '2018-09-28 11:20:55';
let endDate = '2018-10-28 11:20:55';
let test1 = startDate.unix();
let test2 = endDate.unix();
However it gives me an error
ERROR TypeError: Cannot read property 'Unix' of undefined
Can anyone tell me how I can convert datetime to Unix using MomentJS?
The issue is because you're calling unix() on plain strings. You need to instead call it on MomentJS objects. To create those, you can provide the date strings to a MomentJS constructor, like this:
let startDate = '2018-09-28 11:20:55';
let endDate = '2018-10-28 11:20:55';
let test1 = moment(startDate).unix();
let test2 = moment(endDate).unix();
console.log(test1);
console.log(test2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment-with-locales.min.js"></script>

how to convert unix time to unix date by moment.js

Some string like 1487152419798 its a timestamp with date & time
i need string like 1487152419798 a timestamp with date & 00:00.
let timestamp = '1487152419798';
let date = moment(Number(timestamp)).format('YYYY-MM-DD');
let unixDate = moment.unix(date); //don't work
i need 2017.02.15 00:00 in unix
You can use startOf("day") to modify a Moment instance to set all time fields to 0. So first convert timestamp to a number:
timestamp = Number(timestamp)
and then either:
If you want local:
let local = moment(timestamp).startOf("day");
or if you want UTC:
let utc = moment.utc(timestamp).startOf("day");
// Note---------^^^^^
let timestamp = Number('1487152419798');
let local = moment(timestamp).startOf("day");
let utc = moment.utc(timestamp).startOf("day");
console.log("Date in local timezone: ", local.format());
console.log("Date in UTC: ", utc.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>
let timestamp = '1487152419798';
let date = new Date(Number(timestamp));
date should give you the required value.

Categories

Resources