0

I found lots of info on converting a duration object into various formats but it's harder to find info on converting a moment object into a duration in seconds.

This answer offers the following solution: myVar = moment.duration(myVar).asSeconds()

However it doesn't work in my case, myVar is in MM:SS format not HH:MM:SS format so I'm getting an aberrant result. Any idea how to adapt it to my situation?

EDIT: here's some code

this.totalTimeSimulation = moment(lastActionEndTime, 'mm:ss').add(additionalTimeDuration, 'seconds').format('mm:ss')
this.totalTimeSimulationInSeconds = moment.duration(this.totalTimeSimulation).asSeconds()
console.log(this.totalTimeSimulation)
console.log(this.totalTimeSimulationInSeconds)

In console I see:

04:00

14400

Should be:

04:00

240

Because 4 minutes equals 240 seconds, not 14400 seconds. Moment.js thinks I'm giving it a duration in HH:MM:SS format when actually I'm giving it in MM:SS format.

drake035
  • 3,955
  • 41
  • 119
  • 229
  • Which version of moment are you using? Moment [docs](https://momentjs.com/docs/#/durations/creating/) states that `MM:SS` is supported from 2.3.0 (_`moment.duration('23:59'); // added in 2.3.0`_). Can you provide [mcve]? – VincenzoC Jan 10 '20 at 11:17
  • Using 2.24, maybe that's it? Will update now and see how it goes – drake035 Jan 10 '20 at 11:33
  • Uh.. I don't understand, last version is 2.24: https://www.npmjs.com/package/moment, https://github.com/moment/moment/blob/develop/CHANGELOG.md – drake035 Jan 10 '20 at 11:52
  • Can you provide a snippet that shows your issue? Which is the value of `myVar`? Which result do you get? And which is the expected result? – VincenzoC Jan 10 '20 at 11:54
  • Sorry, my first comment is wrong, moment threats input like `04:00` as `HH:MM`, you can prefix `04:00` with `00:` (e.g. `moment.duration('00:' + this.totalTimeSimulation)`) or use [`moment.duration(Object)`](https://momentjs.com/docs/#/durations/creating/) constructor using `minutes` and `seconds` keys. – VincenzoC Jan 10 '20 at 12:10
  • Ah yes perfect, thanks! You can make an answer and I'll accept it if you want – drake035 Jan 10 '20 at 12:13

1 Answers1

0

Moment threats input like 04:00 as HH:MM

The format is an hour, minute, second string separated by colons like 23:59:59. The number of days can be prefixed with a dot separator like so 7.23:59:59. Partial seconds are supported as well 23:59:59.999.

moment.duration('23:59:59');
moment.duration('23:59:59.999');
moment.duration('7.23:59:59.999');
moment.duration('23:59'); // added in 2.3.0

You can prefix your input with 00: or use moment.duration(Object) constructor using minutes and seconds keys:

const totalTimeSimulation = '04:00'
const totalTimeSimulationInSeconds = moment.duration('00:' + totalTimeSimulation).asSeconds()
console.log(totalTimeSimulation)
console.log(totalTimeSimulationInSeconds)
const parts = totalTimeSimulation.split(':')
const seconds = moment.duration({
  minutes: parts[0],
  seconds: parts[1]}).asSeconds()
console.log(seconds)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>
VincenzoC
  • 30,117
  • 12
  • 90
  • 112