Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/loilo/node-php-date
⏰ Use PHP's date() function in JavaScript
https://github.com/loilo/node-php-date
date date-formatting javascript
Last synced: 4 months ago
JSON representation
⏰ Use PHP's date() function in JavaScript
- Host: GitHub
- URL: https://github.com/loilo/node-php-date
- Owner: loilo
- Created: 2016-10-18T13:26:59.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2022-09-29T18:27:39.000Z (over 2 years ago)
- Last Synced: 2024-04-14T10:16:05.005Z (10 months ago)
- Topics: date, date-formatting, javascript
- Language: JavaScript
- Homepage:
- Size: 53.7 KB
- Stars: 2
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# php-date
[](https://github.com/loilo/node-php-date/actions)
[](https://www.npmjs.com/package/php-date)This package aims to mimic the formatting of [PHP's date](http://php.net/manual/en/function.date.php) function in JavaScript.
- [Almost all](#timezone-identifiers) tokens are supported
- Tiny (1.2KB minified & gzipped)
- Type annotations included
- Works in all modern browsers, IE11 and Node.js## Installation
Install via npm:
```bash
npm install --save php-date
```Or use in the browser via [unpkg](https://unpkg.com) (using the global `phpDate` variable):
```html
```
## Usage
The signature looks like this:
```ts
date (formatter: string, date: Date = new Date): string
```So just use it mostly like in PHP:
```js
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:
```js
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.