how to use the secondstohours function from date-fns in javascript

You can use the secondstohours function from date-fns library in JavaScript as shown below:

index.tsx
// Import the function
const { formatDuration, addMinutes } = require('date-fns')

// Convert seconds to hours
const seconds = 7200
const duration = formatDuration({ seconds })
console.log(duration.hours)
205 chars
8 lines

In the above code, we first import the formatDuration function from date-fns. We then set the amount of seconds to convert, 7200 in this case. We then use the formatDuration function to create a duration object that we can extract the number of hours from. Finally, we log the number of hours to the console.

Note that the formatDuration function returns an object with properties for years, months, days, hours, minutes, and seconds. If you need a more precise conversion (e.g., converting seconds to minutes or days), you can use the other properties of the duration object.

gistlibby LogSnag