question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

moment.Duration & format

See original GitHub issue

Hi,

I’ve just tested the changes made to NSwag to handle TimeSpan as moment.Duration in TypeScript (#1460) and there is still a problem in the toJSON function that get’s generated. The following this.myTimeSpan.format('HH:mm:ss') doesn’t work, it seems that moment.Duration has no format function. It has been a long and ongoing discussion on the moment GitHub see 463 & 1048.

The solution seems to be to create a moment.Moment and use format on that, it should be the following code: moment.utc(this.myTimeSpan).format('HH:mm:ss') or to use the Moment Duration Format plugin.

Anyway, an other problem is the loss of days & fractional seconds when doing the format back to Json. This can’t be solved with the trick via moment.utc, and there the Moment Duration Format is needed.

this.myTimeSpan.format('d.hh:mm:ss.SS')

This should be conform the ASP.NET style time spans.

So I would suggest to change the format in code and to require the usage of the Moment Duration Format plugin. To install the plugin the following npm packages are needed:

npm install moment-duration-format
npm install --save-dev @types/moment-duration-format

And I needed to add the following import in the generated file, just after import * as moment from 'moment';:

import 'moment-duration-format';

I hoped there was an easier way, but after a lot of research it was the best I could come up with.

Regards,

Jochen

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
jochenjonccommented, Jul 31, 2018

In the NSwag code after the following line the extra import for moment-duration-format also needs to be added. https://github.com/RSuter/NSwag/blob/2f5d7d35bdc07e76de09220d3f517d83eecbc9fc/src/NSwag.CodeGeneration.TypeScript/Templates/File.liquid#L55

But I’m not sure if there is a possibility to include the line only if TimeSpans are being generated? Because otherwise a lot of existing projects that don’t use TimeSpan will have to install the moment-duration-format package.

1reaction
RicoSutercommented, Jul 31, 2018

Ok, i think it’s fine now…

Read more comments on GitHub >

github_iconTop Results From Across the Web

moment-duration-format
A moment.js plugin for formatting durations.. Latest version: 2.3.2, last published: 4 years ago. Start using moment-duration-format in your ...
Read more >
How to use format() on a moment.js duration?
format('hh:mm:ss '); - A duration is a vector. By putting its tail on the origin you can use date formatting to get hh:mm:ss....
Read more >
Format function plugin for the Moment Duration object.
The duration.format method allows coordinated formatting of multiple moment durations at once. This function accepts an array of durations as its first argument ......
Read more >
Duration Format - momentjs.com - Read the Docs
This is a plugin that will allow comprehensive formatting of Moment Durations. For example, moment.duration(123, "minutes").format("h:mm"); ...
Read more >
Moment.js | Docs
When creating a moment from a string, we first check if the string matches known ISO 8601 formats, we then check if the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found