Easily Format Dates with the Date Format Library

Category: Date & Time , Javascript | June 19, 2024
Views Total:21 views
Official Page:Go to website
Last Update:June 19, 2024


Easily Format Dates with the Date Format Library


This is a lightweight date format JavaScript library that provides an easy way to display dates in a specific format for user interfaces, logs, or data exchange.

It supports both 24-hour and 12-hour time formats with AM/PM indicators. Want to display dates in YYYY-MM-DD or a more casual hh:mm A format? No sweat! Date Format handles it all.

How to use it:

1. Install and import the formatDate with NPM.

$ npm install @vtjhyn/formatdate
import { formatDate } from "@vtjhyn/formatdate";

2. To format a date, call the formatDate function and pass in the date object and the desired format pattern.

const date = new Date();
// 20-06-2024
console.log(formatDate(date, 'DD-MM-YYYY'));
// 2024-06-20
console.log(formatDate(date, 'YYYY-MM-DD'));
// 24-06-20
console.log(formatDate(date, 'YY-MM-DD'));
// 10:51 AM
console.log(formatDate(date, 'hh:mm A'));
// 10:51:29
console.log(formatDate(date, 'HH:mm:ss'));
2024-06-20 10:51:29 AM
console.log(formatDate(date, 'YYYY-MM-DD hh:mm:ss A'));

3. The formatDate function recognizes these format tokens:

  • YYYY: Full year (e.g., 2024)
  • YY: Two-digit year (e.g., 24)
  • MM: Month (01-12)
  • DD: Day of the month (01-31)
  • hh: Hour in 12-hour format (01-12)
  • HH: Hour in 24-hour format (00-23)
  • mm: Minutes (00-59)
  • ss: Seconds (00-59)
  • A: AM/PM indicator

You Might Be Interested In:

Leave a Reply