├── .editorconfig ├── .gitattributes ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/date-time 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | steps: 16 | - uses: actions/checkout@v2 17 | - uses: actions/setup-node@v2 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export interface Options { 2 | /** 3 | Custom date. 4 | 5 | @default new Date() 6 | */ 7 | readonly date?: Date; 8 | 9 | /** 10 | Show the date in the local time zone. 11 | 12 | @default false 13 | */ 14 | readonly local?: boolean; 15 | 16 | /** 17 | Show the UTC time zone postfix. 18 | 19 | @default false 20 | */ 21 | readonly showTimeZone?: boolean; 22 | 23 | /** 24 | Show the milliseconds in the date if any. 25 | 26 | @default false 27 | */ 28 | readonly showMilliseconds?: boolean; 29 | } 30 | 31 | /** 32 | Pretty datetime: `2014-01-09 06:46:01`. 33 | 34 | @example 35 | ``` 36 | import dateTime from 'date-time'; 37 | 38 | dateTime(); 39 | //=> '2017-05-20 17:07:05' 40 | 41 | dateTime({date: new Date(1989, 2, 4, 10)}); 42 | //=> '1989-03-04 09:00:00' 43 | 44 | dateTime({showTimeZone: true}); 45 | //=> '2017-05-20 17:07:05 UTC+7' 46 | 47 | dateTime({local: false}); 48 | //=> '2017-05-20 10:07:05' 49 | 50 | dateTime({local: false, showTimeZone: true}); 51 | //=> '2017-05-20 10:07:05 UTC' 52 | 53 | dateTime({showMilliseconds: true}); 54 | //=> '2017-05-20 17:07:05 6ms' 55 | ``` 56 | */ 57 | export default function dateTime(options?: Options): string; 58 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import timeZone from 'time-zone'; 2 | 3 | export default function dateTime(options = {}) { 4 | let { 5 | date = new Date(), 6 | local = true, 7 | showTimeZone = false, 8 | showMilliseconds = false 9 | } = options; 10 | 11 | if (local) { 12 | // Offset the date so it will return the correct value when getting the ISO string. 13 | date = new Date(date.getTime() - (date.getTimezoneOffset() * 60000)); 14 | } 15 | 16 | let end = ''; 17 | 18 | if (showTimeZone) { 19 | end = ' UTC' + (local ? timeZone(date) : ''); 20 | } 21 | 22 | if (showMilliseconds && date.getUTCMilliseconds() > 0) { 23 | end = ` ${date.getUTCMilliseconds()}ms${end}`; 24 | } 25 | 26 | return date 27 | .toISOString() 28 | .replace(/T/, ' ') 29 | .replace(/\..+/, end); 30 | } 31 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import dateTime from './index.js'; 3 | 4 | expectType(dateTime()); 5 | expectType(dateTime({date: new Date(1989, 2, 4, 10)})); 6 | expectType(dateTime({showTimeZone: true})); 7 | expectType(dateTime({local: false})); 8 | expectType(dateTime({local: false, showTimeZone: true})); 9 | expectType(dateTime({showMilliseconds: true})); 10 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "date-time", 3 | "version": "4.0.0", 4 | "description": "Pretty datetime: `2014-01-09 06:46:01`", 5 | "license": "MIT", 6 | "repository": "sindresorhus/date-time", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": ">=12" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "datetime", 27 | "date-time", 28 | "date", 29 | "time", 30 | "utc", 31 | "iso", 32 | "timezone", 33 | "zone", 34 | "timestamp" 35 | ], 36 | "dependencies": { 37 | "time-zone": "^2.0.0" 38 | }, 39 | "devDependencies": { 40 | "ava": "^3.15.0", 41 | "tsd": "^0.14.0", 42 | "xo": "^0.38.2" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # date-time 2 | 3 | > Pretty datetime: `2014-01-09 06:46:01` 4 | 5 | ## Install 6 | 7 | ``` 8 | $ npm install date-time 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import dateTime from 'date-time'; 15 | 16 | dateTime(); 17 | //=> '2017-05-20 17:07:05' 18 | 19 | dateTime({date: new Date(1989, 2, 4, 10)}); 20 | //=> '1989-03-04 09:00:00' 21 | 22 | dateTime({showTimeZone: true}); 23 | //=> '2017-05-20 17:07:05 UTC+7' 24 | 25 | dateTime({local: false}); 26 | //=> '2017-05-20 10:07:05' 27 | 28 | dateTime({local: false, showTimeZone: true}); 29 | //=> '2017-05-20 10:07:05 UTC' 30 | 31 | dateTime({showMilliseconds: true}); 32 | //=> '2017-05-20 17:07:05 6ms' 33 | ``` 34 | 35 | ## API 36 | 37 | ### dateTime(options?) 38 | 39 | ### options 40 | 41 | Type: `object` 42 | 43 | #### date 44 | 45 | Type: `Date`\ 46 | Default: `new Date()` 47 | 48 | Custom date. 49 | 50 | #### local 51 | 52 | Type: `boolean`\ 53 | Default: `true` 54 | 55 | Show the date in the local time zone. 56 | 57 | #### showTimeZone 58 | 59 | Type: `boolean`\ 60 | Default: `false` 61 | 62 | Show the UTC time zone postfix. 63 | 64 | #### showMilliseconds 65 | 66 | Type: `boolean`\ 67 | Default: `false` 68 | 69 | Show the milliseconds in the date if any. 70 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import dateTime from './index.js'; 3 | 4 | test('main', t => { 5 | t.regex(dateTime(), /^[\d-]+ [\d:]+$/); 6 | t.regex(dateTime({date: new Date()}), /^[\d-]+ [\d:]+$/); 7 | }); 8 | 9 | test('showTimeZone option', t => { 10 | t.regex(dateTime({showTimeZone: true}), /^[\d-]+ [\d:]+ UTC[-+][\d:]+$/); 11 | 12 | t.regex(dateTime({ 13 | local: false, 14 | showTimeZone: true 15 | }), /^[\d-]+ [\d:]+ UTC$/); 16 | }); 17 | 18 | test('showMilliseconds option', t => { 19 | t.regex(dateTime({ 20 | local: false, 21 | showMilliseconds: true 22 | }), /^[\d-]+ [\d:]+ \d+ms$/); 23 | 24 | t.regex(dateTime({ 25 | local: false, 26 | showMilliseconds: true, 27 | showTimeZone: true 28 | }), /^[\d-]+ [\d:]+ \d+ms UTC$/); 29 | }); 30 | --------------------------------------------------------------------------------