Reputation: 1724
Here is my JS:
var startDateTime = '2021-10-19 20:00:00';
var endDateTime = moment(startDateTime).add(2, 'hours').format('YYYY-MM-DD hh:mm:ss');
document.getElementById("startDateTime").innerHTML = startDateTime;
document.getElementById("endDateTime").innerHTML = endDateTime;
I'm expecting endDateTime
to be 2021-10-19 22:00:00
. But it comes out as 2021-10-19 10:00:00
instead. I'm not sure what I'm doing wrong. Here is the JSFiddle: https://jsfiddle.net/0L3zyqxs/
Upvotes: 0
Views: 41
Reputation: 52
To use 24h format in moment.js you need to use HH.
var endDateTime = moment(startDateTime).add(2, 'hours').format('YYYY-MM-DD HH:mm:ss');
Upvotes: 3
Reputation: 648
please try H as below this is for 24 hours
var endDateTime = moment(startDateTime)
.add(2, 'hours')
.format('YYYY-MM-DD HH:mm:ss');
Upvotes: 2