Skip to content
Advertisement

How to combine Date and Time from 2 different var’s in NodeJS

I have a User Input which consists of a Date and a Time Input. Both of those values are send as a full date like

reminder_date: '2021-02-15T08:00:00.000Z',
reminder_time: '2021-02-09T17:00:00.000Z' 

But i want to store it in my db as one single value like

'2021-02-15T17:00:00.000Z'

what is the best approach to achieve that ? split the strings on T and then combine or is there a simple way i can take the date part and the time part and create a new dateTime to save

Advertisement

Answer

If the two items are strings, you could simply use the JavaScript substr function to get the first 11 characters from reminder_date and the second part of reminder_time (starting from 11), then concatenate them, e.g.

let reminder_date = '2021-02-15T08:00:00.000Z';
let reminder_time = '2021-02-09T17:00:00.000Z';
let reminder_date_time = reminder_date.substr(0, 11) + reminder_time.substr(11);
console.log(reminder_date_time);
Advertisement