php-date

Formats a Date object with a formatter string

Usage no npm install needed!

<script type="module">
  import phpDate from 'https://cdn.skypack.dev/php-date';
</script>

README

php-date

Tests Version on npm

This package aims to mimic the formatting of PHP's date function in JavaScript.

  • Almost all tokens are supported
  • Tiny (1.2KB minified & gzipped)
  • Type annotations included
  • Works in all modern browsers, IE11 and Node.js

Installation

Install via npm:

npm install --save php-date

Or use in the browser via unpkg (using the global phpDate variable):

<script src="https://unpkg.com/php-date"></script>

Usage

The signature looks like this:

date (formatter: string, date: Date = new Date): string

So just use it mostly like in PHP:

const date = require('php-date')

const releaseDate = new Date(2016, 9, 18)

date('d.m.Y', releaseDate) // 18.10.2016
date('F jS Y', releaseDate) // October 18th 2016

Sometimes you want to format a given date for the UTC timezone. You can do so by using the date.UTC function, it has the same signature as the date function itself:

date.UTC(...)

The second argument is completely optional; like in PHP this will default to the current point in time.

Timezone Identifiers

The only PHP date tokens not supported by this package are timezones (tokens e and T) which would return timezone identifiers. I felt like that would bloat the code a little too much with the fallbacks necessary for older browsers.