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

You can use the fromUnixTime function from the date-fns library in JavaScript to convert a Unix timestamp to a JavaScript Date object. Here is an example:

const { fromUnixTime } = require('date-fns')

const unixTimestamp = 1559347200 // June 1, 2019 at 12:00:00 AM UTC
const date = fromUnixTime(unixTimestamp)

console.log(date) // Sat Jun 01 2019 00:00:00 GMT-0400 (Eastern Daylight Time)
235 chars
7 lines

In the example above, we import the fromUnixTime function from the date-fns package. We then create a variable called unixTimestamp that contains the Unix timestamp that we want to convert. Finally, we use the fromUnixTime function to convert the timestamp to a Date object and store the result in a variable called date. We then log the date variable to the console.

Note that the fromUnixTime function assumes that the Unix timestamp is expressed in seconds. If your timestamp is expressed in milliseconds, you will need to divide it by 1000 before passing it to the function.

gistlibby LogSnag