Skip to content

Use date-fns to display absolute minutes #3464

Answered by maximtop
niqwithq asked this question in Q&A
Discussion options

You must be logged in to vote

In date-fns, you can handle this as follows:

  const duration = { minutes: 13, seconds: 7 };
  
  // Refer to https://date-fns.org/v2.30.0/docs/formatISODuration for documentation
  const isoDurationStr = formatISODuration(duration);
  console.log(isoDurationStr); // Output: P0Y0M0DT0H13M7S - a valid value for the datetime attribute duration
  
  // Refer to https://date-fns.org/v2.30.0/docs/formatDuration for documentation
  const formattedDuration = formatDuration(duration);
  console.log(formattedDuration); // Output: 13 minutes 7 seconds - you can use whatever format suits you

Here's a CodeSandbox link for this example: https://codesandbox.io/s/date-fns-duration-format-pg2z5k?file=/sr…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@niqwithq
Comment options

Answer selected by niqwithq
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants