You can use the secondstohours
function from date-fns
library in JavaScript as shown below:
index.tsx205 chars8 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