5

I got an ISO 8601 string as duration and I need to format it as XhYm( 1h20m). Does anyone have some suggestions?

What I did right now is this:

const duration = moment.duration(secondData.duration);
const formatted = moment.utc(duration.asMilliseconds()).format('HH:mm');
DKyleo
  • 806
  • 8
  • 11
user8991667
  • 85
  • 2
  • 2
  • 7
  • maybe this helps https://stackoverflow.com/questions/11300278/full-humanized-durations-in-moment-js – David Auvray Jan 09 '19 at 14:07
  • 3
    and what is wrong when you try your code? You didn't make clear. Also please show us the input string from which you want to produce your stated result. – ADyson Jan 09 '19 at 14:08
  • The input string is PT1H20M and I want to format is as 1h20m, with my code it formats as 01:20 @ADyson – user8991667 Jan 09 '19 at 14:10
  • 3
    [link](https://en.wikipedia.org/wiki/ISO_8601#Durations) instead of trying to look smart how about you help me :) – user8991667 Jan 09 '19 at 14:15
  • Actually I'm trying to clarify what you're doing, in order to do exactly that. Ok so it's a duration string, I'm sorry I didn't quite catch that, sorry for misreading. But still, please answer what is `secondData` and how did you populate it? The example is still incomplete. – ADyson Jan 09 '19 at 14:15
  • secondData.duration is the 'PT1H20M'. It parses the string, returns a value as miliseconds and it formats it as 01:20 but I want it as 1h20m. What's there more to clarify? – user8991667 Jan 09 '19 at 14:18
  • If you want to receive a format like "1h20m", why don't you use an appropriate formatting string? Obviously, `HH:mm` does what it looks like... – Nico Haase Jan 09 '19 at 14:19
  • Possible duplicate of [How can I format time durations exactly using Moment.js?](https://stackoverflow.com/questions/21674694/how-can-i-format-time-durations-exactly-using-moment-js) – VincenzoC Jan 10 '19 at 00:02

2 Answers2

15

To get the output format you want, you'll need to set up the format string differently in the format() call:

const duration = moment.duration('PT1H20M');
const formatted = moment.utc(duration.asMilliseconds()).format("H[h]m[m]");

Using the square brackets makes moment print those characters without trying to use them in the format. See the Escaping Characters in the momentjs documentation.

Joseph Erickson
  • 2,304
  • 20
  • 29
2

The simplest way to do it involves a little bit of manual formatting:

var d = moment.duration("PT1H20M");
console.log(d.hours()+"H"+d.minutes()+"M");
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.23.0/moment.min.js"></script>
ADyson
  • 57,178
  • 14
  • 51
  • 63
  • Can you explain why your code looks different than the one by Joseph in the accepted answer? – Nico Haase Jan 09 '19 at 14:26
  • @NicoHaase What do you want to know, specifically? It's just a different way of doing it, using only the duration-related functions, rather than importing the duration into a momentJS object first. It's perhaps less neat, relying on some manual formatting, but it's just an alternative method to get the desired output. – ADyson Jan 09 '19 at 14:30
  • I'm just curious - doesn't `moment.duration("PT1H20M")` already return an object? – Nico Haase Jan 09 '19 at 14:32
  • 2
    @NicoHaase yes, it does. It returns a Duration object, not a Moment object. Not sure what you mean by "already" in this context though? I just used the methods available in the Duration object to create the format, rather than Joseph's approach which was to convert the duration to milliseconds, parse that as a Moment object and then use the moment object's (more sophisticated) formatting options to create the output. Does that answer your query? – ADyson Jan 09 '19 at 14:38