Gurbela
Gurbela

Reputation: 1214

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

Upvotes: 1

Views: 3823

Answers (2)

Stem Florin
Stem Florin

Reputation: 872

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
console.log(endTime)
let startTime = endTime.subtract(duration, 'ms')
console.log(startTime)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.2.1/moment.min.js"></script>

Upvotes: 1

Harshal Bhandari
Harshal Bhandari

Reputation: 99

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')

Upvotes: 1

Related Questions