Logo
Jay Gurav
6:32:56 PM
3 min read

FormatDateTime

Table of Contents

FormaDateTime is a lightweight, high-performance JavaScript/TypeScript utility for formatting Date objects using customizable templates. Parse once, render repeatedly with any date—anytime, anywhere.


Features

  • Template-Based Formatting — Define human-readable output formats using intuitive placeholders.
  • Single Parsing, Multiple Renders — Templates are parsed once, enabling high performance across multiple render calls.
  • Flexible & Easy to Use — Create different formatter instances for different template needs.
  • Written in TypeScript with JavaScript support

Supported Identifiers

PlaceholderDescriptionExample Output
MMMMFull month nameJanuary
MMShort month nameJan
MoNumeric month (zero-padded)01
YYYYFull year2021
YYShort year21
ddddFull weekday nameMonday
DDDay of month (zero-padded)15
DoOrdinal day (st, nd, etc.)15th
hHour in 12-hour format06
HHour in 24-hour format18
mmMinutes (zero-padded)05
ssSeconds (zero-padded)05
paAM/PM markerAM or PM

import FormatDateTime from 'forma-date-time'; // adjust path or name as needed

const formatter = new FormatDateTime(
  "Published on: {Do} {MMMM} {YYYY}",
  {
    padMonth: true,
    padDays: true,
    padHours: true,
  }
);

console.log(formatter.render(new Date()));  
// Output example: "Published on: 24th December 2012"

Options

Customize padding behavior with the following boolean options (defaults shown):

{
  padMonth: true, // zero-pad month numbers (e.g., "01")
  padDays: true,  // zero-pad day numbers (e.g., "05")
  padHours: true, // zero-pad hour numbers (e.g., "06")
}

{
  padMonth: true, // zero-pad month numbers (e.g., "01")
  padDays: true,  // zero-pad day numbers (e.g., "05")
  padHours: true, // zero-pad hour numbers (e.g., "06")
}

Why Use FormaDateTime?

  • Performance: Templates are parsed a single time and reused for multiple date renders.
  • Clarity: Placeholders are easy to write and read.
  • Flexibility: Create multiple template instances for varied formatting scenarios.

Contributing

Contributions are welcome! Whether it’s improving documentation, adding features, or reporting issues—feel free to submit PRs or open issues.


🏛️ License

MIT

-a project by Jay Gurav.