input - 15:30
output - 17:10
I try to do this:
time = '15:30'
moment(time)
.add(40, 'minutes')
.add(2, 'hours')
.format('HH:mm')
but it doesn't work, moment says "invalid date"
CodePudding user response:
First of all you need to convert the string date to a valid date, for example:
const time = '15:30'
const date = moment(time, "HH:mm").toDate();
Then, you can use the date
variable like this:
moment(date)
.add(40, 'minutes')
.add(2, 'hours')
.format('HH:mm')
And finally you will get your date time parsed.
Here is a working version of this:
const time = '15:30'
const date = moment(time, "HH:mm").toDate();
const parsedDate = moment(date)
.add(40, 'minutes')
.add(2, 'hours')
.format('HH:mm')
console.log(parsedDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
CodePudding user response:
Adding 2 hours and 30 minutes to 15:30h will get you 18:10h - also without using moments.js:
const time=new Date(2022,1,1,15,30); // define initial datetime object
time.setHours(time.getHours() 2);
time.setMinutes(time.getMinutes() 40);
console.log(time.toTimeString().slice(0,5));