https://github.com/uiwjs/date-formatter
Get a formatted date.
https://github.com/uiwjs/date-formatter
date formatter javascript js
Last synced: 4 months ago
JSON representation
Get a formatted date.
- Host: GitHub
- URL: https://github.com/uiwjs/date-formatter
- Owner: uiwjs
- License: mit
- Created: 2019-03-07T11:09:42.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-12-15T18:22:54.000Z (about 1 year ago)
- Last Synced: 2025-08-08T23:49:07.668Z (4 months ago)
- Topics: date, formatter, javascript, js
- Language: TypeScript
- Homepage: https://uiwjs.github.io/date-formatter/
- Size: 277 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
Formatter
===
[](https://github.com/uiwjs/date-formatter/actions/workflows/ci.yml) [](https://www.npmjs.com/package/@uiw/formatter) [](https://www.npmjs.com/package/@uiw/formatter)  [](https://coveralls.io/github/uiwjs/date-formatter?branch=master)
[](https://www.npmjs.com/package/@uiw/formatter)
Get a formatted date.
[](https://codepen.io/jaywcjlove/pen/zbZKmq)
[](https://codesandbox.io/s/date-formatter-demo-jib1u)
### Install
```bash
$ npm install --save @uiw/formatter
```
### Usage
```js
import formatter from '@uiw/formatter';
console.log(formatter());
//=> 2019-03-07
console.log(formatter.utc());
//=> 2019-03-07
console.log(formatter('YYYY年MM月DD日', new Date(2019, 3, 7)))
//=> 2019年04月07日
console.log(formatter('YYYY年MM月DD日 16:30:29', new Date(2019, 3, 7, 16, 30, 29)))
//=> 2019年04月07日 16:30:29
console.log(formatter('YYYY年MM月DD日 HH:mm-ss', new Date(2019, 3, 7, 16, 30, 29)))
//=> 2019年04月07日 16:30-29
console.log(formatter('YYYY/MM/DD HH:mm:ss', new Date(2019, 3, 7, 16, 30, 29)))
//=> 2019年04月07日 16:30-29
console.log(formatter('YYYY/MM/DD HH:mm:ss', new Date(2019, 3, 7, 16, 30, 29)))
//=> 2019/04/07 16:30:29
console.log(formatter('YYYY'));
//=> 2019
console.log(formatter.utc('YYYY'));
//=> 2019
```
Or manually download and link **formatter.js** in your HTML, It can also be downloaded via [UNPKG](https://unpkg.com/@uiw/formatter):
```html
document.getElementById('date').innerHTML = formatter();
```
The above [example preview](https://codepen.io/jaywcjlove/pen/zbZKmq).
### timeZoneConverter
Resolve changes in time zone, resulting in inaccurate display server time
```js
function timeZoneConverter(date, timeZone) {
const oldDate = new Date(date);
const newDate = new Date();
const stamp = oldDate.getTime();
if (!timeZone) return oldDate;
return (isNaN(timeZone) && !timeZone)
? oldDate :
new Date(stamp + (newDate.getTimezoneOffset() * 60 * 1000) + (timeZone * 60 * 60 * 1000));
}
timeZoneConverter(new Date(1434701732*1000), 8)
```
## API
```js
formatter(rule: String, date: Date, utc: Boolean);
formatter.utc(rule: String, date: Date);
```
## Supported Patterns
| rule | Description | 中文说明 | E.g |
|--------- |-------- |--------- |-------- |
| `YYYY` | full year | 年 | `2019` |
| `MM` | month | 月 | `02` |
| `DD` | day | 天 | `05` |
| `HH` | hours | 时 | `12` |
| `mm` | minutes | 分钟 | `59` |
| `ss` | seconds | 秒 | `09` |
| `ms` | milliseconds | 毫秒 | `532` |