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

To use the minutestohours function from the date-fns library in TypeScript, you can follow these steps:

  1. Install date-fns and its type declarations:
npm install date-fns
npm install -D @types/date-fns
52 chars
3 lines
  1. Import the minutestohours function and its type from date-fns:
index.ts
import { minutesToHours, MinutesToHoursResult } from 'date-fns';
65 chars
2 lines
  1. Use the minutesToHours function:
index.ts
const minutes = 120;
const result: MinutesToHoursResult = minutesToHours(minutes);
console.log(result.hours); // Output: 2
console.log(result.minutes); // Output: 0
165 chars
5 lines

The minutesToHours function takes a number of minutes as its argument and returns an object with the hours and minutes properties. The MinutesToHoursResult type describes this object.

Here's a complete example that you can run in a TypeScript sandbox:

index.ts
import { minutesToHours, MinutesToHoursResult } from 'date-fns';

const minutes = 120;
const result: MinutesToHoursResult = minutesToHours(minutes);
console.log(result.hours); // Output: 2
console.log(result.minutes); // Output: 0
231 chars
7 lines

gistlibby LogSnag