简体   繁体   中英

How to subtract Time from current Time momentjs

I have

const endTime = moment().format("HH:mm:ss");
const duration = moment.utc(request.input('duration') * 60000).format("HH:mm:ss");

I want to calculate

startTime = endTime - duration
endTime.subtract(duration);

Should do the trick ,here is the docs if you want to dig deeper docs

Edit:

const endTime = moment(); //now time
const duration = request.input('duration') //duration in miliseconds
let startTime = endTime.subtract(duration,'ms')

 const endTime = moment(); //now time const duration = 600000; //hardcoded 10 mins //duration in miliseconds let startTime = endTime.subtract(duration, 'ms') console.log(endTime) console.log(startTime) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script> 

You can use endTime.subtract(duration, 'ms'). But for that endTime has to be a moment object. So format the answer later.

const endTime = moment();
const duration = request.input('duration')
let startTime = endTime.subtract(duration,'ms').format('HH:mm:ss')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM