Skip to content
Merged

V2 #18

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions .eslintrc.js

This file was deleted.

22 changes: 15 additions & 7 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ on:
tags:
- 'v*.*.*'

permissions:
id-token: write # Required for OIDC
contents: read

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@v1
- uses: actions/checkout@v6
- uses: actions/setup-node@v6
with:
node-version: 12
node-version: 24.x
registry-url: https://registry.npmjs.org/
- run: npm install
- run: npm version "${GITHUB_REF:11}" --git-tag-version false --commit-hooks false
- run: npm publish --access public
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
- run: |
VERSION="${GITHUB_REF:11}"
npm version "$VERSION" --git-tag-version false --commit-hooks false
if [[ "$VERSION" == *"-"* ]]; then
npm publish --access public --tag next
else
npm publish --access public
fi
12 changes: 7 additions & 5 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ on:
branches:
- '*' # matches every branch
- '*/*' # matches every branch containing a single '/'
pull_request:
jobs:
eslint:
name: eslint
quality:
name: quality
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v6
- name: Use Node.js
uses: actions/setup-node@v1
uses: actions/setup-node@v6
with:
node-version: 12.x
node-version: 24.x
- run: npm install
- run: npm run eslint
- run: npm test
125 changes: 85 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,60 +1,105 @@
# Date Formatter
# @date-js/date-formatter

- A light javascript tool for formatting date (<3kB).
- Zero dependency.
- TypeScript compatible.
[![npm](https://img.shields.io/npm/v/@date-js/date-formatter)](https://www.npmjs.com/package/@date-js/date-formatter)
[![license](https://img.shields.io/npm/l/@date-js/date-formatter)](LICENSE)

## Example
**Format a date. Nothing else. Under 1KB gzipped.**

``` javascript
DateFormatter.format('%l %j %F %Y', new Date());
// Sunday 12 October 2014
No config. No plugins. No dependencies. Locale support out of the box via the native `Intl` API.

```javascript
DateFormatter.format('%l, %F %j %Y', new Date());
// Sunday, March 29 2026

DateFormatter.format('%l %j %F %Y', new Date(), 'fr-FR');
// dimanche 29 mars 2026
```

``` javascript
DateFormatter.format('%l %j %F %Y', new Date(), 'fr_FR');
// Samedi 12 octobre 2014
## Why this lib?

If all you need is to display a date, there's no point in shipping a full date toolkit. Perfect for landing pages, lightweight widgets, or any project where pulling in a full date library would be overkill.

- **Under 1KB gzipped** — no dead code, no bloat
- **Zero dependencies** — nothing to audit, nothing to break
- **Locale support out of the box** — automatically uses the browser language, no config needed
- **Familiar syntax** — `%Y-%m-%d` style, like PHP's `date()` or Python's `strftime`

## Install & Usage

### With a bundler (webpack, Vite, Rollup…)

```bash
npm install @date-js/date-formatter
```

By default, Date Formatter uses the current browser language. You can force a locale like this:
``` javascript
Use the default export to get a ready-to-use singleton:

```javascript
import DateFormatter from '@date-js/date-formatter';

DateFormatter.format('%d/%m/%Y', new Date()); // 29/03/2026
DateFormatter.setLocale('fr-FR');
DateFormatter.format('%l %j %F %Y', new Date()); // dimanche 29 mars 2026
```

You can see examples in example directory.
Or import the class directly to create isolated instances (useful when managing multiple locales):

## Install it
```javascript
import { DateFormatter } from '@date-js/date-formatter';

### With NPM
const fr = new DateFormatter('fr-FR');
const en = new DateFormatter('en-US');

``` bash
npm install @date-js/date-formatter --save
fr.format('%l %j %F %Y', new Date()); // dimanche 29 mars 2026
en.format('%l %j %F %Y', new Date()); // Sunday March 29 2026
```

``` javascript
import DateFormatter from '@date-js/date-formatter';
### In a browser — classic script tag

The UMD bundle exposes `DateFormatter` as a global:

```html
<script src="https://cdn.jsdelivr.net/npm/@date-js/date-formatter@2/dist/date-formatter.
js"></script>
<script>
DateFormatter.format('%d/%m/%Y', new Date()); // 29/03/2026
</script>
```

### Otherwise
### In a browser — ES module

Modern browsers support `<script type="module">` without any bundler:

```html
<script type="module">
import DateFormatter from 'https://cdn.jsdelivr.
net/npm/@date-js/date-formatter@2/dist/date-formatter.js';

``` html
<script src="https://cdn.jsdelivr.net/npm/@date-js/date-formatter@1.0/dist/date-formatter.js"></script>
DateFormatter.format('%d/%m/%Y', new Date()); // 29/03/2026
</script>
```

## Format

| Character | Description | Example |
| ------------- |:-------------:| -----:|
| %Y | A full numeric representation of a year, 4 digits | 2020 |
| %y | A two digit representation of a year | 20 |
| %d | Day of the month, 2 digits with leading zeros | 01 to 31 |
| %l | A full textual representation of the day of the week | Sunday through Saturday |
| %D | A textual representation of a day, three letters | Mon through Sun |
| %F | A full textual representation of a month, such as January or March | January through December |
| %M | A short textual representation of a month, three letters | Jan through Dec |
| %m | Numeric representation of a month, with leading zeros | 01 through 12 |
| %n | Numeric representation of a month, without leading zeros | 1 through 12 |
| %G | 24-hour format of an hour without leading zeros | 0 through 23 |
| %H | 24-hour format of an hour with leading zeros | 00 through 23 |
| %i | Minutes with leading zeros | 00 to 59 |
| %s | Seconds with leading zeros | 00 to 59 |
## Format specifiers

| Specifier | Description | Example |
|---|---|---|
| `%Y` | Year, 4 digits | `2026` |
| `%y` | Year, 2 digits | `26` |
| `%d` | Day of the month, with leading zero | `01` – `31` |
| `%j` | Day of the month, without leading zero | `1` – `31` |
| `%l` | Full weekday name *(locale-aware)* | `Sunday`, `dimanche` |
| `%D` | Short weekday name *(locale-aware)* | `Sun`, `dim.` |
| `%F` | Full month name *(locale-aware)* | `March`, `mars` |
| `%M` | Short month name *(locale-aware)* | `Mar`, `mars` |
| `%m` | Month, with leading zero | `01` – `12` |
| `%n` | Month, without leading zero | `1` – `12` |
| `%H` | Hour (24h), with leading zero | `00` – `23` |
| `%G` | Hour (24h), without leading zero | `0` – `23` |
| `%i` | Minutes, with leading zero | `00` – `59` |
| `%s` | Seconds, with leading zero | `00` – `59` |

Escape a `%` with a backslash: `\%`.

## License

MIT
11 changes: 11 additions & 0 deletions bin/docker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

DIRECTORY=$(dirname $(realpath $0 ))

TTY_FLAG=$([ -t 0 ] && echo "-it" || echo "-i")
docker run $TTY_FLAG --rm \
-v "$DIRECTORY/..":/home/node/app \
-w /home/node/app \
-u $(id -u ${USER}):$(id -g ${USER}) \
node:24-slim \
"${@:-bash}"
12 changes: 12 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const tseslint = require('typescript-eslint');

module.exports = tseslint.config(
...tseslint.configs.recommended,
{
files: ['**/*.ts'],
rules: {
'quotes': ['error', 'single'],
'semi': 'error',
},
}
);
63 changes: 30 additions & 33 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,41 @@
{
"name": "@date-js/date-formatter",
"version": "0.0.0-version",
"description": "A light javascript tool for formatting date.",
"description": "Tiny date formatter with locale support. Zero dependencies, under 1KB gzipped.",
"main": "dist/date-formatter.js",
"exports": {
".": {
"import": "./dist/date-formatter.js",
"require": "./dist/date-formatter.js",
"types": "./dist/types/index.d.ts"
}
},
"scripts": {
"prepublishOnly": "webpack",
"eslint": "eslint ./src --ext .ts,.js,.d.ts",
"eslint-fix": "eslint ./src --ext .ts,.js,.d.ts --fix",
"eslint": "eslint src",
"eslint-fix": "eslint src --fix",
"watch": "webpack --watch",
"build": "webpack"
"build": "webpack",
"test": "vitest run"
},
"repository": {
"type": "git",
"url": "git+https://github.com/date-js/date-formatter.git"
},
"keywords": [
"date",
"light",
"small",
"tiny",
"format",
"formatter"
"formatter",
"date-format",
"strftime",
"intl",
"locale",
"i18n",
"lightweight",
"tiny",
"zero-dependency",
"browser",
"typescript"
],
"files": [
"dist"
Expand All @@ -33,30 +48,12 @@
},
"homepage": "https://github.com/date-js/date-formatter#readme",
"devDependencies": {
"@babel/core": "^7.14.6",
"@babel/preset-env": "^7.14.7",
"@babel/preset-typescript": "^7.14.5",
"@types/node": "^12.12.62",
"@typescript-eslint/eslint-plugin": "^2.34.0",
"@typescript-eslint/parser": "^2.34.0",
"babel-loader": "^8.2.2",
"eslint": "^6.8.0",
"eslint-config-airbnb-base": "^14.2.0",
"eslint-config-standard": "^14.1.1",
"eslint-import-resolver-webpack": "^0.12.2",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-promise": "^4.3.1",
"path": "^0.12.7",
"terser-webpack-plugin": "^5.1.4",
"ts-loader": "^9.2.3",
"typescript": "^4.3.4",
"webpack": "^5.41.0",
"webpack-cli": "^4.7.2"
},
"babel": {
"presets": [
"@babel/preset-env",
"@babel/preset-typescript"
]
"eslint": "^10.1.0",
"ts-loader": "^9.5.4",
"typescript": "^5.9.3",
"typescript-eslint": "^8.57.2",
"vitest": "^4.1.2",
"webpack": "^5.105.4",
"webpack-cli": "^7.0.2"
}
}
Loading