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

To use the addhours function from the date-fns library in TypeScript, you must first install the library as a dependency in your project:

npm install date-fns
21 chars
2 lines

Once installed, you can import the addHours function in your TypeScript code and use it to add hours to a Date object:

index.ts
import { addHours } from 'date-fns';

const myDate = new Date('2021-05-23T18:25:00Z'); // initialize a Date object
const hoursToAdd = 3;

const resultDate = addHours(myDate, hoursToAdd); // add 3 hours to myDate

console.log(`Result: ${resultDate}`); // print the result to the console
286 chars
9 lines

In this example, the addHours function takes two parameters: a Date object and the number of hours to add. The function returns a new Date object with the added hours. The result can be stored in a variable and used in your code as needed.

gistlibby LogSnag