How to use date-fns In Express
Date-fns is a modern JavaScript date utility library that provides a wide range of functions for manipulating and formatting dates. To use date-fns with TypeScript, first, install the library:
npm install date-fns
Example: Importing the format function from date-fns, creating a Date object for the current date, then formatting it as “dd MMM yyyy” (’01 May 2024′).
import { format } from 'date-fns';
const currentDate: Date = new Date();
const formattedDate: string = format(currentDate, 'dd MMM yyyy');
console.log(formattedDate);
Output:
01 May 2024
How to express a Date Type in TypeScript ?
In this tutorial, we will learn about expressing the date type in TypeScript. Generally, In JavaScript, the classes can be used to add types similarly in TypeScript the classes, type alias, and the interfaces can also be used to add types to the variables.
Some ways of expressing date type in TypeScript are listed below:
Table of Content
- Without specifying any of the types
- Manually Typing Date Object
- Using type alias or interface for typing
- Using date-fns