├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .npmrc ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── helpers.js ├── index.d.ts ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org/ 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | indent_size = 2 8 | indent_style = space 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "eslint:recommended" 4 | ], 5 | 6 | "env": { 7 | "browser": false, 8 | "es6": true, 9 | "node": true, 10 | "mocha": true 11 | }, 12 | 13 | "parserOptions":{ 14 | "ecmaVersion": 9, 15 | "sourceType": "module", 16 | "ecmaFeatures": { 17 | "modules": true, 18 | "experimentalObjectRestSpread": true 19 | } 20 | }, 21 | 22 | "globals": { 23 | "document": false, 24 | "navigator": false, 25 | "window": false 26 | }, 27 | 28 | "rules": { 29 | "accessor-pairs": 2, 30 | "arrow-spacing": [2, { "before": true, "after": true }], 31 | "block-spacing": [2, "always"], 32 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 33 | "comma-dangle": [2, "never"], 34 | "comma-spacing": [2, { "before": false, "after": true }], 35 | "comma-style": [2, "last"], 36 | "constructor-super": 2, 37 | "curly": [2, "multi-line"], 38 | "dot-location": [2, "property"], 39 | "eol-last": 2, 40 | "eqeqeq": [2, "allow-null"], 41 | "generator-star-spacing": [2, { "before": true, "after": true }], 42 | "handle-callback-err": [2, "^(err|error)$" ], 43 | "indent": [2, 2, { "SwitchCase": 1 }], 44 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 45 | "keyword-spacing": [2, { "before": true, "after": true }], 46 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 47 | "new-parens": 2, 48 | "no-array-constructor": 2, 49 | "no-caller": 2, 50 | "no-class-assign": 2, 51 | "no-cond-assign": 2, 52 | "no-const-assign": 2, 53 | "no-control-regex": 2, 54 | "no-debugger": 2, 55 | "no-delete-var": 2, 56 | "no-dupe-args": 2, 57 | "no-dupe-class-members": 2, 58 | "no-dupe-keys": 2, 59 | "no-duplicate-case": 2, 60 | "no-empty-character-class": 2, 61 | "no-eval": 2, 62 | "no-ex-assign": 2, 63 | "no-extend-native": 2, 64 | "no-extra-bind": 2, 65 | "no-extra-boolean-cast": 2, 66 | "no-extra-parens": [2, "functions"], 67 | "no-fallthrough": 2, 68 | "no-floating-decimal": 2, 69 | "no-func-assign": 2, 70 | "no-implied-eval": 2, 71 | "no-inner-declarations": [2, "functions"], 72 | "no-invalid-regexp": 2, 73 | "no-irregular-whitespace": 2, 74 | "no-iterator": 2, 75 | "no-label-var": 2, 76 | "no-labels": 2, 77 | "no-lone-blocks": 2, 78 | "no-mixed-spaces-and-tabs": 2, 79 | "no-multi-spaces": 2, 80 | "no-multi-str": 2, 81 | "no-multiple-empty-lines": [2, { "max": 1 }], 82 | "no-native-reassign": 0, 83 | "no-negated-in-lhs": 2, 84 | "no-new": 2, 85 | "no-new-func": 2, 86 | "no-new-object": 2, 87 | "no-new-require": 2, 88 | "no-new-wrappers": 2, 89 | "no-obj-calls": 2, 90 | "no-octal": 2, 91 | "no-octal-escape": 2, 92 | "no-proto": 0, 93 | "no-redeclare": 2, 94 | "no-regex-spaces": 2, 95 | "no-return-assign": 2, 96 | "no-self-compare": 2, 97 | "no-sequences": 2, 98 | "no-shadow-restricted-names": 2, 99 | "no-spaced-func": 2, 100 | "no-sparse-arrays": 2, 101 | "no-this-before-super": 2, 102 | "no-throw-literal": 2, 103 | "no-trailing-spaces": 0, 104 | "no-undef": 2, 105 | "no-undef-init": 2, 106 | "no-unexpected-multiline": 2, 107 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 108 | "no-unreachable": 2, 109 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 110 | "no-useless-call": 0, 111 | "no-with": 2, 112 | "one-var": [0, { "initialized": "never" }], 113 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 114 | "padded-blocks": [0, "never"], 115 | "quotes": [2, "single", "avoid-escape"], 116 | "radix": 2, 117 | "semi": [2, "always"], 118 | "semi-spacing": [2, { "before": false, "after": true }], 119 | "space-before-blocks": [2, "always"], 120 | "space-before-function-paren": [2, "never"], 121 | "space-in-parens": [2, "never"], 122 | "space-infix-ops": 2, 123 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 124 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 125 | "use-isnan": 2, 126 | "valid-typeof": 2, 127 | "wrap-iife": [2, "any"], 128 | "yoda": [2, "never"] 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | .nyc* 10 | 11 | # npm 12 | node_modules 13 | npm-debug.log 14 | 15 | # yarn 16 | yarn.lock 17 | yarn-error.log 18 | 19 | # misc 20 | _gh_pages 21 | _draft 22 | _drafts 23 | bower_components 24 | vendor 25 | temp 26 | tmp 27 | TODO.md 28 | package-lock.json -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '10' 9 | - '9' 10 | - '8' 11 | - '7' 12 | - '6' 13 | - '5' 14 | - '4' 15 | - '0.12' 16 | - '0.10' 17 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | ```js 4 | const timestamp = require('{%= name %}'); 5 | 6 | console.log(timestamp()); 7 | //=> {%= timestamp() %} 8 | 9 | console.log(timestamp.utc()); 10 | //=> {%= timestampUTC() %} 11 | ``` 12 | 13 | ## Customizing the timestamp 14 | 15 | You may also pass a string to format the generated timestamp. 16 | 17 | ```js 18 | console.log(timestamp('YYYYMMDD')); 19 | //=> {%= timestamp('YYYYMMDD') %} 20 | 21 | console.log(timestamp.utc('YYYYMMDD')); 22 | //=> {%= timestampUTC('YYYYMMDD') %} 23 | ``` 24 | 25 | **Supported patterns** 26 | 27 | - `YYYY`: full year (ex: **{%= timestamp("YYYY") %}**) 28 | - `MM`: month (ex: **04**) 29 | - `DD`: day (ex: **01**) 30 | - `HH`: hours (ex: **12**) 31 | - `mm`: minutes (ex: **59**) 32 | - `ss`: seconds (ex: **09**) 33 | - `ms`: milliseconds (ex: **532**) 34 | 35 | **Usage Examples** 36 | 37 | ```js 38 | console.log(timestamp('YYYYMMDD')); 39 | //=> {%= timestamp('YYYYMMDD') %} 40 | console.log(timestamp.utc('YYYYMMDD')); 41 | //=> {%= timestampUTC('YYYYMMDD') %} 42 | 43 | console.log(timestamp('YYYYMMDD:ss')); 44 | //=> {%= timestamp('YYYYMMDD:ss') %} 45 | console.log(timestamp.utc('YYYYMMDD:ss')); 46 | //=> {%= timestampUTC('YYYYMMDD:ss') %} 47 | 48 | console.log(timestamp('YYYY/MM/DD:mm:ss')); 49 | //=> {%= timestamp('YYYY/MM/DD:mm:ss') %} 50 | console.log(timestamp.utc('YYYY/MM/DD:mm:ss')); 51 | //=> {%= timestampUTC('YYYY/MM/DD:mm:ss') %} 52 | 53 | console.log(timestamp('YYYY:MM:DD')); 54 | //=> {%= timestamp('YYYY:MM:DD') %} 55 | console.log(timestamp.utc('YYYY:MM:DD')); 56 | //=> {%= timestampUTC('YYYY:MM:DD') %} 57 | 58 | console.log(timestamp('[YYYY:MM:DD]')); 59 | //=> {%= timestamp('[YYYY:MM:DD]') %} 60 | console.log(timestamp.utc('[YYYY:MM:DD]')); 61 | //=> {%= timestampUTC('[YYYY:MM:DD]') %} 62 | 63 | console.log(timestamp('YYYY/MM/DD')); 64 | //=> {%= timestamp('YYYY/MM/DD') %} 65 | console.log(timestamp.utc('YYYY/MM/DD')); 66 | //=> {%= timestampUTC('YYYY/MM/DD') %} 67 | 68 | console.log(timestamp('YYYY:MM')); 69 | //=> {%= timestamp('YYYY:MM') %} 70 | console.log(timestamp.utc('YYYY:MM')); 71 | //=> {%= timestampUTC('YYYY:MM') %} 72 | 73 | console.log(timestamp('YYYY')); 74 | //=> {%= timestamp('YYYY') %} 75 | console.log(timestamp.utc('YYYY')); 76 | //=> {%= timestampUTC('YYYY') %} 77 | 78 | console.log(timestamp('MM')); 79 | //=> {%= timestamp('MM') %} 80 | console.log(timestamp.utc('MM')); 81 | //=> {%= timestampUTC('MM') %} 82 | 83 | console.log(timestamp('DD')); 84 | //=> {%= timestamp('DD') %} 85 | console.log(timestamp.utc('DD')); 86 | //=> {%= timestampUTC('DD') %} 87 | 88 | console.log(timestamp('HH')); 89 | //=> {%= timestamp('HH') %} 90 | console.log(timestamp.utc('HH')); 91 | //=> {%= timestampUTC('HH') %} 92 | 93 | console.log(timestamp('mm')); 94 | //=> {%= timestamp('mm') %} 95 | console.log(timestamp.utc('mm')); 96 | //=> {%= timestampUTC('mm') %} 97 | 98 | console.log(timestamp('ss')); 99 | //=> {%= timestamp('ss') %} 100 | console.log(timestamp.utc('ss')); 101 | //=> {%= timestampUTC('ss') %} 102 | 103 | console.log(timestamp('ms')); 104 | //=> {%= timestamp('ms') %} 105 | console.log(timestamp.utc('ms')); 106 | //=> {%= timestampUTC('ms') %} 107 | ``` 108 | 109 | ## Release history 110 | 111 | ### v2.0.0 112 | 113 | **Breaking changes** 114 | 115 | Default pattern was changed from `YYYY:MM:DD` to `YYYY-MM-DD`. See [issues/3](../../issues) for more details. 116 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015-present, Jon Schlinkert. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # time-stamp [![NPM version](https://img.shields.io/npm/v/time-stamp.svg?style=flat)](https://www.npmjs.com/package/time-stamp) [![NPM monthly downloads](https://img.shields.io/npm/dm/time-stamp.svg?style=flat)](https://npmjs.org/package/time-stamp) [![NPM total downloads](https://img.shields.io/npm/dt/time-stamp.svg?style=flat)](https://npmjs.org/package/time-stamp) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/time-stamp.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/time-stamp) 2 | 3 | > Get a formatted timestamp. 4 | 5 | Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. 6 | 7 | - [Install](#install) 8 | - [Usage](#usage) 9 | - [Customizing the timestamp](#customizing-the-timestamp) 10 | - [Release history](#release-history) 11 | * [v2.0.0](#v200) 12 | - [About](#about) 13 | 14 | _(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_ 15 | 16 | ## Install 17 | 18 | Install with [npm](https://www.npmjs.com/): 19 | 20 | ```sh 21 | $ npm install --save time-stamp 22 | ``` 23 | 24 | ## Usage 25 | 26 | ```js 27 | const timestamp = require('time-stamp'); 28 | 29 | console.log(timestamp()); 30 | //=> 2018-10-26 31 | 32 | console.log(timestamp.utc()); 33 | //=> 2018-10-26 34 | ``` 35 | 36 | ## Customizing the timestamp 37 | 38 | You may also pass a string to format the generated timestamp. 39 | 40 | ```js 41 | console.log(timestamp('YYYYMMDD')); 42 | //=> 20181026 43 | 44 | console.log(timestamp.utc('YYYYMMDD')); 45 | //=> 20181026 46 | ``` 47 | 48 | **Supported patterns** 49 | 50 | * `YYYY`: full year (ex: **2018**) 51 | * `MM`: month (ex: **04**) 52 | * `DD`: day (ex: **01**) 53 | * `HH`: hours (ex: **12**) 54 | * `mm`: minutes (ex: **59**) 55 | * `ss`: seconds (ex: **09**) 56 | * `ms`: milliseconds (ex: **532**) 57 | 58 | **Usage Examples** 59 | 60 | ```js 61 | console.log(timestamp('YYYYMMDD')); 62 | //=> 20181026 63 | console.log(timestamp.utc('YYYYMMDD')); 64 | //=> 20181026 65 | 66 | console.log(timestamp('YYYYMMDD:ss')); 67 | //=> 20181026:24 68 | console.log(timestamp.utc('YYYYMMDD:ss')); 69 | //=> 20181026:24 70 | 71 | console.log(timestamp('YYYY/MM/DD:mm:ss')); 72 | //=> 2018/10/26:46:24 73 | console.log(timestamp.utc('YYYY/MM/DD:mm:ss')); 74 | //=> 2018/10/26:46:24 75 | 76 | console.log(timestamp('YYYY:MM:DD')); 77 | //=> 2018:10:26 78 | console.log(timestamp.utc('YYYY:MM:DD')); 79 | //=> 2018:10:26 80 | 81 | console.log(timestamp('[YYYY:MM:DD]')); 82 | //=> [2018:10:26] 83 | console.log(timestamp.utc('[YYYY:MM:DD]')); 84 | //=> [2018:10:26] 85 | 86 | console.log(timestamp('YYYY/MM/DD')); 87 | //=> 2018/10/26 88 | console.log(timestamp.utc('YYYY/MM/DD')); 89 | //=> 2018/10/26 90 | 91 | console.log(timestamp('YYYY:MM')); 92 | //=> 2018:10 93 | console.log(timestamp.utc('YYYY:MM')); 94 | //=> 2018:10 95 | 96 | console.log(timestamp('YYYY')); 97 | //=> 2018 98 | console.log(timestamp.utc('YYYY')); 99 | //=> 2018 100 | 101 | console.log(timestamp('MM')); 102 | //=> 10 103 | console.log(timestamp.utc('MM')); 104 | //=> 10 105 | 106 | console.log(timestamp('DD')); 107 | //=> 26 108 | console.log(timestamp.utc('DD')); 109 | //=> 26 110 | 111 | console.log(timestamp('HH')); 112 | //=> 00 113 | console.log(timestamp.utc('HH')); 114 | //=> 04 115 | 116 | console.log(timestamp('mm')); 117 | //=> 46 118 | console.log(timestamp.utc('mm')); 119 | //=> 46 120 | 121 | console.log(timestamp('ss')); 122 | //=> 24 123 | console.log(timestamp.utc('ss')); 124 | //=> 24 125 | 126 | console.log(timestamp('ms')); 127 | //=> 186 128 | console.log(timestamp.utc('ms')); 129 | //=> 186 130 | ``` 131 | 132 | ## Release history 133 | 134 | ### v2.0.0 135 | 136 | **Breaking changes** 137 | 138 | Default pattern was changed from `YYYY:MM:DD` to `YYYY-MM-DD`. See [issues/3](../../issues) for more details. 139 | 140 | ## About 141 | 142 |
143 | Contributing 144 | 145 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 146 | 147 |
148 | 149 |
150 | Running Tests 151 | 152 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 153 | 154 | ```sh 155 | $ npm install && npm test 156 | ``` 157 | 158 |
159 | 160 |
161 | Building docs 162 | 163 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 164 | 165 | To generate the readme, run the following command: 166 | 167 | ```sh 168 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 169 | ``` 170 | 171 |
172 | 173 | ### Related projects 174 | 175 | You might also be interested in these projects: 176 | 177 | * [days](https://www.npmjs.com/package/days): Days of the week. | [homepage](https://github.com/jonschlinkert/days "Days of the week.") 178 | * [iso-week](https://www.npmjs.com/package/iso-week): Get the ISO week of the year. | [homepage](https://github.com/jonschlinkert/iso-week "Get the ISO week of the year.") 179 | * [month](https://www.npmjs.com/package/month): Get the name or number of the current month or any month of the year. | [homepage](https://github.com/datetime/month "Get the name or number of the current month or any month of the year.") 180 | * [months](https://www.npmjs.com/package/months): Months of the year. | [homepage](https://github.com/datetime/months "Months of the year.") 181 | * [o-clock](https://www.npmjs.com/package/o-clock): Simple javascript utility for displaying the time in 12-hour clock format. | [homepage](https://github.com/jonschlinkert/o-clock "Simple javascript utility for displaying the time in 12-hour clock format.") 182 | * [seconds](https://www.npmjs.com/package/seconds): Get the number of seconds for a minute, hour, day and week. | [homepage](https://github.com/jonschlinkert/seconds "Get the number of seconds for a minute, hour, day and week.") 183 | * [week](https://www.npmjs.com/package/week): Get the current week number. | [homepage](https://github.com/datetime/week "Get the current week number.") 184 | * [weekday](https://www.npmjs.com/package/weekday): Get the name and number of the current weekday. Or get the name of the… [more](https://github.com/datetime/weekday) | [homepage](https://github.com/datetime/weekday "Get the name and number of the current weekday. Or get the name of the weekday for a given number.") 185 | * [year](https://www.npmjs.com/package/year): Simple utility to get the current year with 2 or 4 digits. | [homepage](https://github.com/jonschlinkert/year "Simple utility to get the current year with 2 or 4 digits.") 186 | 187 | ### Contributors 188 | 189 | | **Commits** | **Contributor** | 190 | | --- | --- | 191 | | 31 | [jonschlinkert](https://github.com/jonschlinkert) | 192 | | 7 | [doowb](https://github.com/doowb) | 193 | | 1 | [evocateur](https://github.com/evocateur) | 194 | | 1 | [mendenhallmagic](https://github.com/mendenhallmagic) | 195 | | 1 | [mvanroon](https://github.com/mvanroon) | 196 | | 1 | [leesei](https://github.com/leesei) | 197 | | 1 | [sleagon](https://github.com/sleagon) | 198 | 199 | ### Author 200 | 201 | **Jon Schlinkert** 202 | 203 | * [GitHub Profile](https://github.com/jonschlinkert) 204 | * [Twitter Profile](https://twitter.com/jonschlinkert) 205 | * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) 206 | 207 | ### License 208 | 209 | Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert). 210 | Released under the [MIT License](LICENSE). 211 | 212 | *** 213 | 214 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on October 26, 2018._ -------------------------------------------------------------------------------- /helpers.js: -------------------------------------------------------------------------------- 1 | const timestamp = require('./'); 2 | 3 | exports.timestamp = timestamp; 4 | exports.timestampUTC = timestamp.utc; 5 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare function timestamp(pattern?: string | Date, date?: Date): string 2 | declare function timestampUTC(pattern?: string | Date, date?: Date): string 3 | 4 | export default timestamp 5 | export { timestampUTC as utc }; 6 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * time-stamp 3 | * 4 | * Copyright (c) 2015-2018, Jon Schlinkert. 5 | * Released under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var dateRegex = /(?=(YYYY|YY|MM|DD|HH|mm|ss|ms))\1([:\/]*)/g; 11 | var timespan = { 12 | YYYY: ['getFullYear', 4], 13 | YY: ['getFullYear', 2], 14 | MM: ['getMonth', 2, 1], // getMonth is zero-based, thus the extra increment field 15 | DD: ['getDate', 2], 16 | HH: ['getHours', 2], 17 | mm: ['getMinutes', 2], 18 | ss: ['getSeconds', 2], 19 | ms: ['getMilliseconds', 3] 20 | }; 21 | 22 | var timestamp = function(str, date, utc) { 23 | if (typeof str !== 'string') { 24 | date = str; 25 | str = 'YYYY-MM-DD'; 26 | } 27 | 28 | if (!date) date = new Date(); 29 | return str.replace(dateRegex, function(match, key, rest) { 30 | var args = timespan[key]; 31 | var name = args[0]; 32 | var chars = args[1]; 33 | if (utc === true) name = 'getUTC' + name.slice(3); 34 | var val = '00' + String(date[name]() + (args[2] || 0)); 35 | return val.slice(-chars) + (rest || ''); 36 | }); 37 | }; 38 | 39 | timestamp.utc = function(str, date) { 40 | return timestamp(str, date, true); 41 | }; 42 | 43 | module.exports = timestamp; 44 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "time-stamp", 3 | "description": "Get a formatted timestamp.", 4 | "version": "2.2.0", 5 | "homepage": "https://github.com/jonschlinkert/time-stamp", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "contributors": [ 8 | "Daniel Stockman (http://evocateur.org)", 9 | "Drew (https://github.com/mendenhallmagic)", 10 | "Jon Schlinkert (http://twitter.com/jonschlinkert)", 11 | "leesei (https://leesei.github.io)" 12 | ], 13 | "repository": "jonschlinkert/time-stamp", 14 | "bugs": { 15 | "url": "https://github.com/jonschlinkert/time-stamp/issues" 16 | }, 17 | "license": "MIT", 18 | "files": [ 19 | "index.js", 20 | "index.d.ts" 21 | ], 22 | "main": "index.js", 23 | "types": "index.d.ts", 24 | "engines": { 25 | "node": ">=0.10.0" 26 | }, 27 | "scripts": { 28 | "test": "mocha" 29 | }, 30 | "devDependencies": { 31 | "gulp-format-md": "^1.0.0", 32 | "mocha": "^3.5.3", 33 | "pad-left": "^2.1.0" 34 | }, 35 | "keywords": [ 36 | "console", 37 | "date", 38 | "format", 39 | "formatting", 40 | "log", 41 | "pretty", 42 | "stamp", 43 | "terminal", 44 | "time", 45 | "time-stamp" 46 | ], 47 | "verb": { 48 | "run": true, 49 | "toc": true, 50 | "layout": "default", 51 | "tasks": [ 52 | "readme" 53 | ], 54 | "helpers": [ 55 | "./helpers.js" 56 | ], 57 | "plugins": [ 58 | "gulp-format-md" 59 | ], 60 | "related": { 61 | "list": [ 62 | "days", 63 | "iso-week", 64 | "month", 65 | "months", 66 | "o-clock", 67 | "seconds", 68 | "week", 69 | "weekday", 70 | "year" 71 | ] 72 | }, 73 | "lint": { 74 | "reflinks": true 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var assert = require('assert'); 5 | var pad = require('pad-left'); 6 | var timestamp = require('./'); 7 | var timestampUTC = require('./').utc; 8 | 9 | describe('timestamp', function() { 10 | it('should return the default timestamp:', function() { 11 | assert(/^\d{4}-\d{2}-\d{2}$/.test(timestamp())); 12 | }); 13 | 14 | it('should work with delimiters', function() { 15 | assert(/^\[\d{4}\]/.test(timestamp('[YYYY]'))); 16 | assert(/^\[\d{4}\d{2}\]/.test(timestamp('[YYYYMM]'))); 17 | assert(/^\[\d{4}:\d{2}\]/.test(timestamp('[YYYY:MM]'))); 18 | }); 19 | 20 | it('should work with no separators', function() { 21 | assert(/^\d{4}\d{2}$/.test(timestamp('YYYYMM'))); 22 | assert(/^\d{4}\d{2}\d{2}$/.test(timestamp('YYYYMMDD'))); 23 | assert(/^\d{4}\d{2}\d{2}\d{2}$/.test(timestamp('YYYYMMDDss'))); 24 | assert(/^\d{4}\d{2}\d{2}$/.test(timestamp('YYYYMMss'))); 25 | }); 26 | 27 | it('should return the year:', function() { 28 | assert(/^\d{4}$/.test(timestamp('YYYY'))); 29 | assert(/^\d{4}$/.test(timestampUTC('YYYY'))); 30 | }); 31 | 32 | it('should return the month:', function() { 33 | assert(/^\d{2}$/.test(timestamp('MM'))); 34 | assert(/^\d{2}$/.test(timestampUTC('MM'))); 35 | }); 36 | 37 | it('should return the day:', function() { 38 | assert(/^\d{2}$/.test(timestamp('DD'))); 39 | assert(/^\d{2}$/.test(timestampUTC('DD'))); 40 | }); 41 | 42 | it('should return hours:', function() { 43 | assert(/^\d{2}$/.test(timestamp('HH'))); 44 | assert(/^\d{2}$/.test(timestampUTC('HH'))); 45 | }); 46 | 47 | it('should return minutes:', function() { 48 | assert(/^\d{2}$/.test(timestamp('mm'))); 49 | assert(/^\d{2}$/.test(timestampUTC('mm'))); 50 | }); 51 | 52 | it('should return seconds:', function() { 53 | assert(/^\d{2}$/.test(timestamp('ss'))); 54 | assert(/^\d{2}$/.test(timestampUTC('ss'))); 55 | }); 56 | 57 | it('should return miliseconds:', function() { 58 | assert(/^\d{3}$/.test(timestamp('ms'))); 59 | assert(/^\d{3}$/.test(timestampUTC('ms'))); 60 | }); 61 | 62 | it('should increment zero-based month:', function() { 63 | var expected = String(new Date().getUTCMonth() + 1); 64 | assert.equal(timestamp('MM'), pad(expected, 2, '0')); 65 | }); 66 | 67 | it('should not increment one-based methods:', function() { 68 | var expected = String(new Date().getUTCFullYear()); 69 | assert.equal(timestamp('YYYY'), pad(expected, 4, '0')); 70 | }); 71 | 72 | it('should return the 2 digit year for a given date', function() { 73 | var date = new Date(2019, 0); 74 | var expectedYear = "19"; 75 | 76 | assert.equal(timestamp('YY', date), expectedYear); 77 | assert.equal(timestampUTC('YY', date), expectedYear); 78 | }); 79 | 80 | }); 81 | --------------------------------------------------------------------------------