how to use the fromunixtime function from the date-fns library in typescript

To use the fromunixtime function from the date-fns library in TypeScript, follow the steps below:

  1. Install the date-fns library by running the following command in your terminal:
index.ts
npm install date-fns
21 chars
2 lines
  1. Import the fromUnixTime function from the date-fns library:
index.ts
import { fromUnixTime } from 'date-fns';
41 chars
2 lines
  1. Use the fromUnixTime function to convert a Unix timestamp to a Date object:
index.ts
const unixTimestamp = 1631852342;
const date = fromUnixTime(unixTimestamp);
console.log(date);
95 chars
4 lines

This will output the following Date object:

index.ts
Thu Sep 16 2021 15:25:42 GMT+0200 (Central European Summer Time)
65 chars
2 lines

That's it! You can now use the fromUnixTime function from the date-fns library to convert Unix timestamps to Date objects in your TypeScript projects.

gistlibby LogSnag