4.0.1 • Published 3 years ago
php-date v4.0.1
php-date
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-dateOr 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): stringSo 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 2016Sometimes 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.