How do I combine these variables in my code :
var date1 = "13:38";
var date2 = "2019-05-14T00:00:00"
I want to add the time from date1
to date2
, so the result should be "2019-05-14T13:38:00"
How do I combine these variables in my code :
var date1 = "13:38";
var date2 = "2019-05-14T00:00:00"
I want to add the time from date1
to date2
, so the result should be "2019-05-14T13:38:00"
It's not clear what you are trying to do. If the date and time timestamp always has a time of 00:00:00 and you just want to update the time component, you can do that as a string.
Since "2019-05-14T00:00:00" has no offset, it should be parsed as local, so will represent a different moment in time for each different location with a different offset. If it should be parsed as UTC, please make that clear in the OP. It's fairly easy to go either way, you just have to make it clear how it should be treated.
I've allowed for an optional seconds component.
var date1 = "13:38";
var date2 = "2019-05-14T00:00:00";
let [h, m, s] = date1.split(':');
console.log(date2.substring(0,11) + h + ':' + m + ':' + (s||'00'));
However, if the date and time timestamp also has a non–zero time component and you need to add time, a Date object can make life easier:
// Parse ISO 8601 timestamp in YYYY-MM-DDTHH:mm:ss format
// Assumes no offset, parses as local
function parseDateTime(dt) {
let [Y, M, D, h, m, s] = dt.split(/\D/);
return new Date(Y, M-1, D, h, m, s);
}
// Add time in hh:mm[:ss] format to a date
// Returns a new date, doesn't modify date passed in
function addTime(date, time) {
let [h, m, s] = time.split(/\D/);
let d = new Date(+date);
d.setHours(d.getHours() + +h,
d.getMinutes() + +m,
d.getSeconds() + +m);
return d;
}
let date1 = "13:38";
let date2 = "2019-05-14T03:42:28";
let d = parseDateTime(date2);
let e = addTime(d, date1);
// UTC timestamp in ISO 8601 format
console.log(e.toISOString());
Note that in the second example, the displayed value uses the built–in toISOString method that shifts the offset to UTC (+0). If you want the ISO format but in the local timezone, then you can manually format the string. There are lots of questions and answers on how to do that.
There are also numerous libraries that can help. Since the Date object hasn't changed much in over 20 years, any that have been around a while should be OK.
You will have to parse the first timesting and construct a new date object from it. You should construct a UTC object and pass that into the constructor.
For the second datestring, you will need to add the Zulu token to the end to donate a UTC time and make it ISO 8601 compliant.
After you have your two date objects, update the minutes and hours of the second date object, because that one already stores date information.
const date1 = "13:38";
const date2 = "2019-05-14T00:00:00";
const dateObj1 = (tokens =>
new Date(Date.UTC(0, 0, 0, tokens[0], tokens[1])))
(date1.split(':'));
const dateObj2 = new Date(date2 + 'Z');
dateObj2.setUTCMinutes(dateObj2.getUTCMinutes() + dateObj1.getUTCMinutes());
dateObj2.setUTCHours(dateObj2.getUTCHours() + dateObj1.getUTCHours());
console.log(dateObj2.toISOString().substring(0, 19)); // 2019-05-14T13:38:00
If you want to do this in Moment, you can try the following:
const date1 = "13:38";
const date2 = "2019-05-14T00:00:00";
const moment1 = moment.utc(date1, 'HH:mm');
const moment2 = moment.utc(date2)
.add(moment1.minutes(), 'minutes')
.add(moment1.hours(), 'hours')
console.log(moment2.format('YYYY-MM-DDTHH:mm:ss'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js"></script>