├── .npmignore ├── test ├── weekofyear │ ├── test_weekofyear.js │ └── test_weekofyear.sh ├── test_mask-o.js ├── test_mask-p.js ├── test_dayofweek.js ├── test_isoutcdatetime.js ├── test_quotes.js ├── test_mask-mm.js ├── test_mask-ddd.js ├── test_mask-mmm.js ├── test_mask-s.js ├── test_mask-dddd.js ├── test_mask-mmmm.js ├── test_mask-s_.js ├── test_mask-w_.js ├── test_mask-ww_.js ├── test_mask-m.js ├── test_mask-d.js ├── test_mask-yy.js ├── test_mask-dd.js ├── test_mask-h.js ├── test_mask-t.js ├── test_mask-t_.js ├── test_mask-h_.js ├── test_mask-m_.js ├── test_mask-h_h_.js ├── test_mask-hh.js ├── test_mask-l.js ├── test_mask-m_m_.js ├── test_mask-ss.js ├── test_mask-t_t_.js ├── test_mask-tt.js ├── test_mask-n_.js ├── test_mask-l_.js ├── test_mask-yyyy.js ├── test_formats.js └── test_threedays.js ├── .github ├── dependabot.yml └── workflows │ └── nodejs.yml ├── .gitignore ├── package.json ├── benchmark ├── benchmark.js └── previousDateFormat.js ├── LICENSE ├── CONTRIBUTING.md ├── lib └── dateformat.js └── Readme.md /.npmignore: -------------------------------------------------------------------------------- 1 | test 2 | .travis.yml 3 | benchmark 4 | CONTRIBUTING.md 5 | .gitignore 6 | babel.config.json 7 | -------------------------------------------------------------------------------- /test/weekofyear/test_weekofyear.js: -------------------------------------------------------------------------------- 1 | var dateFormat = require('../lib/dateformat.js'); 2 | 3 | var val = process.argv[2] || new Date(); 4 | console.log(dateFormat(val, 'W')); 5 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: github-actions 4 | directory: "/" 5 | schedule: 6 | interval: daily 7 | open-pull-requests-limit: 10 8 | - package-ecosystem: npm 9 | directory: "/" 10 | schedule: 11 | interval: daily 12 | open-pull-requests-limit: 10 13 | -------------------------------------------------------------------------------- /test/test_mask-o.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'o'", function () { 5 | it("should get timezone for any date as something like [+-]XXXX", function (done) { 6 | var date = new Date(); 7 | var d = dateFormat(date, "o"); 8 | assert.ok(d.match(/^[+-]\d{4}$/), d); 9 | done(); 10 | }); 11 | }); 12 | -------------------------------------------------------------------------------- /test/test_mask-p.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'p'", function () { 5 | it("should get timezone for any date as something like [+-]XX:XX", function (done) { 6 | var date = new Date(); 7 | var d = dateFormat(date, "p"); 8 | assert.ok(d.match(/^[+-]\d{2}:\d{2}$/), d); 9 | done(); 10 | 11 | console.log( dateFormat(date, "isoDateTime")); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /test/test_dayofweek.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var dateFormat = require('./../lib/dateformat'); 4 | 5 | describe('dayOfWeek', function() { 6 | it('should correctly format the timezone part', function(done) { 7 | var start = 10; // the 10 of March 2013 is a Sunday 8 | for(var dow = 1; dow <= 7; dow++){ 9 | var date = new Date('2013-03-' + (start + dow)); 10 | var N = dateFormat(date, 'N'); 11 | assert.strictEqual(N, String(dow)); 12 | } 13 | done(); 14 | }); 15 | }); 16 | -------------------------------------------------------------------------------- /test/test_isoutcdatetime.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | 3 | var dateFormat = require("./../lib/dateformat"); 4 | 5 | describe("isoUtcDateTime", function () { 6 | it("should correctly format the timezone part", function (done) { 7 | var actual = dateFormat("2014-06-02T13:23:21-08:00", "isoUtcDateTime"); 8 | assert.strictEqual(actual, "2014-06-02T21:23:21Z"); 9 | var epochTime = dateFormat(0, "isoUtcDateTime"); 10 | assert.strictEqual(epochTime, "1970-01-01T00:00:00Z"); 11 | done(); 12 | }); 13 | }); 14 | -------------------------------------------------------------------------------- /test/test_quotes.js: -------------------------------------------------------------------------------- 1 | var assert = require('assert'); 2 | 3 | var dateFormat = require('./../lib/dateformat'); 4 | 5 | describe('quoted substrings', function() { 6 | var az = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; 7 | it('should not format single quoted substrings removing quotes', function() { 8 | var result = dateFormat("'" + az + "'"); 9 | assert.strictEqual(result, az); 10 | }); 11 | 12 | it('should not format double quoted substrings removing quotes', function() { 13 | var result = dateFormat('"' + az + '"'); 14 | assert.strictEqual(result, az); 15 | }); 16 | }); 17 | 18 | -------------------------------------------------------------------------------- /test/test_mask-mm.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'mm'", function () { 5 | it("should format '2014-11-17' as '11'", function (done) { 6 | var date = new Date("2014-11-17"); 7 | var d = dateFormat(date, "mm"); 8 | assert.strictEqual(d, "11"); 9 | done(); 10 | }); 11 | 12 | it("should format '1992-02-11' as '02'", function (done) { 13 | var date = new Date("1992-02-11"); 14 | var d = dateFormat(date, "mm"); 15 | assert.strictEqual(d, "02"); 16 | done(); 17 | }); 18 | 19 | it("should format '2077-01-25' as '01'", function (done) { 20 | var date = new Date("2077-01-25"); 21 | var d = dateFormat(date, "mm"); 22 | assert.strictEqual(d, "01"); 23 | done(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /test/test_mask-ddd.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'ddd'", function () { 5 | it("should format '2023-01-07' as 'Sat'", function (done) { 6 | var date = new Date("2023-01-07"); 7 | var d = dateFormat(date, "ddd"); 8 | assert.strictEqual(d, "Sat"); 9 | done(); 10 | }); 11 | 12 | it("should format '1873-12-17' as 'Wed'", function (done) { 13 | var date = new Date("1873-12-17"); 14 | var d = dateFormat(date, "ddd"); 15 | assert.strictEqual(d, "Wed"); 16 | done(); 17 | }); 18 | 19 | it("should format '2112-10-25' as 'Tue'", function (done) { 20 | var date = new Date("2112-10-25"); 21 | var d = dateFormat(date, "ddd"); 22 | assert.strictEqual(d, "Tue"); 23 | done(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /test/test_mask-mmm.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'mmm'", function () { 5 | it("should format '2099-1-11' as 'Jan'", function (done) { 6 | var date = new Date("2099-1-11"); 7 | var d = dateFormat(date, "mmm"); 8 | assert.strictEqual(d, "Jan"); 9 | done(); 10 | }); 11 | 12 | it("should format '1982-10-01' as 'Oct'", function (done) { 13 | var date = new Date("1982-10-01"); 14 | var d = dateFormat(date, "mmm"); 15 | assert.strictEqual(d, "Oct"); 16 | done(); 17 | }); 18 | 19 | it("should format '1871-03-22' as 'Mar'", function (done) { 20 | var date = new Date("1871-03-22"); 21 | var d = dateFormat(date, "mmm"); 22 | assert.strictEqual(d, "Mar"); 23 | done(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /test/test_mask-s.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 's'", function () { 5 | it("should format '1993-10-08T10:31:40.811' as '40'", function (done) { 6 | var date = new Date("1993-10-08T10:31:40.811"); 7 | var d = dateFormat(date, "s"); 8 | assert.strictEqual(d, "40"); 9 | done(); 10 | }); 11 | 12 | it("should format '2020-10-25T01:29:02.327' as '2'", function (done) { 13 | var date = new Date("2020-10-25T01:29:02.327"); 14 | var d = dateFormat(date, "s"); 15 | assert.strictEqual(d, "2"); 16 | done(); 17 | }); 18 | 19 | it("should format '2003-07-02T01:29:00.327' as '0'", function (done) { 20 | var d = dateFormat("2003-07-02T01:29:00.327", "s"); 21 | assert.strictEqual(d, "0"); 22 | done(); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /test/test_mask-dddd.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'dddd'", function () { 5 | it("should format '1934-11-13' as 'Tuesday'", function (done) { 6 | var date = new Date("1934-11-13"); 7 | var d = dateFormat(date, "dddd"); 8 | assert.strictEqual(d, "Tuesday"); 9 | done(); 10 | }); 11 | 12 | it("should format '1834-01-2' as 'Thursday'", function (done) { 13 | var date = new Date("1834-01-2"); 14 | var d = dateFormat(date, "dddd"); 15 | assert.strictEqual(d, "Thursday"); 16 | done(); 17 | }); 18 | 19 | it("should format '2077-7-22' as 'Thursday'", function (done) { 20 | var date = new Date("2077-7-22"); 21 | var d = dateFormat(date, "dddd"); 22 | assert.strictEqual(d, "Thursday"); 23 | done(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /test/test_mask-mmmm.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'mmmm'", function () { 5 | it("should format '1993-02-11' as 'February'", function (done) { 6 | var date = new Date("1993-02-11"); 7 | var d = dateFormat(date, "mmmm"); 8 | assert.strictEqual(d, "February"); 9 | done(); 10 | }); 11 | 12 | it("should format '2023-11-13' as 'November'", function (done) { 13 | var date = new Date("2023-11-13"); 14 | var d = dateFormat(date, "mmmm"); 15 | assert.strictEqual(d, "November"); 16 | done(); 17 | }); 18 | 19 | it("should format '2077-10-01' as 'October'", function (done) { 20 | var date = new Date("2077-10-01"); 21 | var d = dateFormat(date, "mmmm"); 22 | assert.strictEqual(d, "October"); 23 | done(); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # .gitignore 2 | # 3 | # Copyright (c) 2014 Charlike Mike Reagent, contributors. 4 | # Released under the MIT license. 5 | # 6 | 7 | # Always-ignore dirs # 8 | # #################### 9 | _gh_pages 10 | node_modules 11 | bower_components 12 | components 13 | vendor 14 | build 15 | dest 16 | dist 17 | coverage 18 | nbproject 19 | cache 20 | temp 21 | tmp 22 | 23 | # Packages # 24 | # ########## 25 | *.7z 26 | *.dmg 27 | *.gz 28 | *.iso 29 | *.jar 30 | *.rar 31 | *.tar 32 | *.zip 33 | 34 | # OS, Logs and databases # 35 | # ######################### 36 | *.pid 37 | *.dat 38 | *.log 39 | *.sql 40 | *.sqlite 41 | *~ 42 | ~* 43 | 44 | # Another files # 45 | # ############### 46 | Icon? 47 | .DS_Store* 48 | Thumbs.db 49 | ehthumbs.db 50 | Desktop.ini 51 | npm-debug.log 52 | .directory 53 | ._* 54 | 55 | koa-better-body 56 | -------------------------------------------------------------------------------- /test/weekofyear/test_weekofyear.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # this just takes php's date() function as a reference to check if week of year 4 | # is calculated correctly in the range from 1970 .. 2038 by brute force... 5 | 6 | SEQ="seq" 7 | SYSTEM=`uname` 8 | if [ "$SYSTEM" = "Darwin" ]; then 9 | SEQ="jot" 10 | fi 11 | 12 | for YEAR in {1970..2038}; do 13 | for MONTH in {1..12}; do 14 | DAYS=$(cal $MONTH $YEAR | egrep "28|29|30|31" |tail -1 |awk '{print $NF}') 15 | for DAY in $( $SEQ $DAYS ); do 16 | DATE=$YEAR-$MONTH-$DAY 17 | echo -n $DATE ... 18 | NODEVAL=$(node test_weekofyear.js $DATE) 19 | PHPVAL=$(php -r "echo intval(date('W', strtotime('$DATE')));") 20 | if [ "$NODEVAL" -ne "$PHPVAL" ]; then 21 | echo "MISMATCH: node: $NODEVAL vs php: $PHPVAL for date $DATE" 22 | else 23 | echo " OK" 24 | fi 25 | done 26 | done 27 | done 28 | -------------------------------------------------------------------------------- /test/test_mask-s_.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'S'", function () { 5 | it("should format '1984-02-7' as 'th'", function (done) { 6 | var date = new Date("1984-02-7"); 7 | var d = dateFormat(date, "S"); 8 | assert.strictEqual(d, "th"); 9 | done(); 10 | }); 11 | 12 | it("should format '2013-01-3' as 'rd'", function (done) { 13 | var date = new Date("2013-01-3"); 14 | var d = dateFormat(date, "S"); 15 | assert.strictEqual(d, "rd"); 16 | done(); 17 | }); 18 | 19 | it("should format '2034-11-22' as 'nd'", function (done) { 20 | var d = dateFormat("2034-11-22", "S"); 21 | assert.strictEqual(d, "nd"); 22 | done(); 23 | }); 24 | 25 | it("should format '2002-02-1' as 'st'", function (done) { 26 | var d = dateFormat("2002-02-1", "S"); 27 | assert.strictEqual(d, "st"); 28 | done(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/test_mask-w_.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'W'", function () { 5 | it("should format '1876-03-22' as '12'", function (done) { 6 | var date = new Date("1876-03-22"); 7 | var d = dateFormat(date, "W"); 8 | assert.strictEqual(d, "12"); 9 | done(); 10 | }); 11 | 12 | it("should format '2013-12-11' as '50'", function (done) { 13 | var date = new Date("2013-12-11"); 14 | var d = dateFormat(date, "W"); 15 | assert.strictEqual(d, "50"); 16 | done(); 17 | }); 18 | 19 | it("should format '2020-08-29' as '35'", function (done) { 20 | var d = dateFormat("2020-08-29", "W"); 21 | assert.strictEqual(d, "35"); 22 | done(); 23 | }); 24 | 25 | it("should format '2020-09-22' as '39'", function (done) { 26 | var d = dateFormat("2020-09-22", "W"); 27 | assert.strictEqual(d, "39"); 28 | done(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/test_mask-ww_.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'WW'", function () { 5 | it("should format '1876-01-12' as '02'", function (done) { 6 | var date = new Date("1876-01-12"); 7 | var d = dateFormat(date, "WW"); 8 | assert.strictEqual(d, "02"); 9 | done(); 10 | }); 11 | 12 | it("should format '2013-12-11' as '50'", function (done) { 13 | var date = new Date("2013-12-11"); 14 | var d = dateFormat(date, "WW"); 15 | assert.strictEqual(d, "50"); 16 | done(); 17 | }); 18 | 19 | it("should format '2020-03-04' as '10'", function (done) { 20 | var d = dateFormat("2020-03-04", "WW"); 21 | assert.strictEqual(d, "10"); 22 | done(); 23 | }); 24 | 25 | it("should format '2020-02-01' as '05'", function (done) { 26 | var d = dateFormat("2020-02-01", "WW"); 27 | assert.strictEqual(d, "05"); 28 | done(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/test_mask-m.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'm'", function () { 5 | it("should format '1974-02-7' as '2'", function (done) { 6 | var date = new Date("1974-02-7"); 7 | var d = dateFormat(date, "m"); 8 | assert.strictEqual(d, "2"); 9 | done(); 10 | }); 11 | 12 | it("should format '1992-09-03' as '9'", function (done) { 13 | var date = new Date("1992-09-03"); 14 | var d = dateFormat(date, "m"); 15 | assert.strictEqual(d, "9"); 16 | done(); 17 | }); 18 | 19 | it("should format '2043-12-22' as '12'", function (done) { 20 | var date = new Date("2043-12-22"); 21 | var d = dateFormat(date, "m"); 22 | assert.strictEqual(d, "12"); 23 | done(); 24 | }); 25 | 26 | it("should format '1800-01-01' as '1'", function (done) { 27 | var date = new Date("1800-01-01"); 28 | var d = dateFormat(date, "m"); 29 | assert.strictEqual(d, "1"); 30 | done(); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@matteo.collina/dateformat", 3 | "description": "A node.js package for Steven Levithan's excellent dateFormat() function. A commonjs module.", 4 | "maintainers": [ 5 | "Matteo Collina " 6 | ], 7 | "homepage": "https://github.com/mcollina/dateformat", 8 | "author": "Steven Levithan", 9 | "contributors": [ 10 | "Steven Levithan", 11 | "Felix Geisendörfer ", 12 | "Christoph Tavan ", 13 | "Jon Schlinkert (https://github.com/jonschlinkert)", 14 | "Matteo Collina " 15 | ], 16 | "version": "5.0.1", 17 | "license": "MIT", 18 | "main": "lib/dateformat", 19 | "devDependencies": { 20 | "mocha": "^9.0.0" 21 | }, 22 | "scripts": { 23 | "test": "mocha", 24 | "benchmark": "node ./benchmark/benchmark.js" 25 | }, 26 | "repository": { 27 | "type": "git", 28 | "url": "https://github.com/mcollina/dateformat.git" 29 | }, 30 | "dependencies": {} 31 | } 32 | -------------------------------------------------------------------------------- /test/test_mask-d.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'd'", function () { 5 | it("should format '1993-03-12' as '12'", function (done) { 6 | var date = new Date("1993-03-12"); 7 | var d = dateFormat(date, "d"); 8 | assert.strictEqual(d, "12"); 9 | done(); 10 | }); 11 | 12 | it("should format '2020-11-1' as '1'", function (done) { 13 | var date = new Date("2020-11-1"); 14 | var d = dateFormat(date, "d"); 15 | assert.strictEqual(d, "1"); 16 | done(); 17 | }); 18 | 19 | it("should format '1830-01-20' as '20'", function (done) { 20 | var date = new Date("1830-01-20"); 21 | var d = dateFormat(date, "d"); 22 | assert.strictEqual(d, "20"); 23 | done(); 24 | }); 25 | 26 | it("should not format '1830-01-06' as '06'", function (done) { 27 | var date = new Date("1830-01-20"); 28 | var d = dateFormat(date, "d"); 29 | assert.notStrictEqual(d, "06"); 30 | done(); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /test/test_mask-yy.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'yy'", function () { 5 | it("should format '1789-11-12' as '89'", function (done) { 6 | var date = new Date("1789-11-12"); 7 | var d = dateFormat(date, "yy"); 8 | assert.strictEqual(d, "89"); 9 | done(); 10 | }); 11 | 12 | it("should format '2089-10-2' as '89'", function (done) { 13 | var date = new Date("2089-10-2"); 14 | var d = dateFormat(date, "yy"); 15 | assert.strictEqual(d, "89"); 16 | done(); 17 | }); 18 | 19 | it("should format '2000-02-7' as '00'", function (done) { 20 | var date = new Date("2000-02-7"); 21 | var d = dateFormat(date, "yy"); 22 | assert.strictEqual(d, "00"); 23 | done(); 24 | }); 25 | 26 | it("should format '1999-11-27' as '99'", function (done) { 27 | var date = new Date("1999-11-27"); 28 | var d = dateFormat(date, "yy"); 29 | assert.strictEqual(d, "99"); 30 | done(); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /benchmark/benchmark.js: -------------------------------------------------------------------------------- 1 | var previousDateFormat = require("./previousDateFormat"); 2 | var newDateFormat = require("../lib/dateformat"); 3 | 4 | const masks = [ 5 | "d", 6 | "W", 7 | "o", 8 | "N", 9 | "shortDate", 10 | "fullDate", 11 | "longTime", 12 | "default", 13 | ]; 14 | let results = []; 15 | 16 | masks.forEach((mask) => { 17 | const previousSpeed = getSpeed(false, mask); 18 | const newSpeed = getSpeed(true, mask); 19 | results.push({ 20 | mask: mask, 21 | previous: previousSpeed + "ms", 22 | new: newSpeed + "ms", 23 | improvement: Math.round((previousSpeed / newSpeed - 1) * 100, 2) + "%", 24 | }); 25 | }); 26 | 27 | function getSpeed(newVersion, mask) { 28 | const startTime = new Date(); 29 | const date = new Date(); 30 | for (var i = 0; i < 100_000; i++) { 31 | if (newVersion) newDateFormat(date, mask); 32 | else previousDateFormat(date, mask); 33 | } 34 | const endTime = new Date(); 35 | return endTime - startTime; 36 | } 37 | 38 | console.table(results); 39 | -------------------------------------------------------------------------------- /test/test_mask-dd.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'dd'", function () { 5 | it("should format '2003-9-11' as '11'", function (done) { 6 | var date = new Date("2003-9-11"); 7 | var d = dateFormat(date, "dd"); 8 | assert.strictEqual(d, "11"); 9 | done(); 10 | }); 11 | 12 | it("should format '1992-02-2' as '02'", function (done) { 13 | var date = new Date("1992-02-2"); 14 | var d = dateFormat(date, "dd"); 15 | assert.strictEqual(d, "02"); 16 | done(); 17 | }); 18 | 19 | it("should format '1032-12-07' as '07'", function (done) { 20 | var date = new Date("1032-12-07"); 21 | var d = dateFormat(date, "dd"); 22 | assert.strictEqual(d, "07"); 23 | done(); 24 | }); 25 | 26 | it("should not format '2077-10-06' as '6'", function (done) { 27 | var date = new Date("2077-10-06"); 28 | var d = dateFormat(date, "dd"); 29 | assert.notStrictEqual(d, "6"); 30 | done(); 31 | }); 32 | }); 33 | -------------------------------------------------------------------------------- /.github/workflows/nodejs.yml: -------------------------------------------------------------------------------- 1 | name: Node.js CI 2 | 3 | on: 4 | push: 5 | paths-ignore: 6 | - 'docs/**' 7 | - '*.md' 8 | pull_request: 9 | paths-ignore: 10 | - 'docs/**' 11 | - '*.md' 12 | 13 | jobs: 14 | test: 15 | runs-on: ${{ matrix.os }} 16 | strategy: 17 | matrix: 18 | os: [ubuntu-latest, windows-latest, macos-latest] 19 | node-version: [12.x, 14.x, 16.x] 20 | 21 | steps: 22 | - uses: actions/checkout@v2.3.4 23 | - name: Use Node.js ${{ matrix.node-version }} 24 | uses: actions/setup-node@v2.4.0 25 | with: 26 | node-version: ${{ matrix.node-version }} 27 | - run: node --version 28 | - run: npm --version 29 | - run: npm install 30 | - run: npm test 31 | env: 32 | CI: true 33 | 34 | automerge: 35 | needs: test 36 | runs-on: ubuntu-latest 37 | steps: 38 | - uses: fastify/github-action-merge-dependabot@v2.4.0 39 | with: 40 | github-token: ${{ secrets.GITHUB_TOKEN }} 41 | -------------------------------------------------------------------------------- /test/test_mask-h.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'h'", function () { 5 | it("should format '2020-10-22T22:10:59.736' as '10'", function (done) { 6 | var date = new Date("2020-10-22T22:10:59.736"); 7 | var d = dateFormat(date, "h"); 8 | assert.strictEqual(d, "10"); 9 | done(); 10 | }); 11 | 12 | it("should format '2020-10-13T13:30:41.278' as '1'", function (done) { 13 | var date = new Date("2020-10-13T13:30:41.278"); 14 | var d = dateFormat(date, "h"); 15 | assert.strictEqual(d, "1"); 16 | done(); 17 | }); 18 | 19 | it("should format '1993-02-19T03:18:18.711' as '3'", function (done) { 20 | var d = dateFormat("1993-02-19T03:18:18.711", "h"); 21 | assert.strictEqual(d, "3"); 22 | done(); 23 | }); 24 | 25 | it("should format '2134-01-25T02:20:42.816' as '2'", function (done) { 26 | var d = dateFormat("2134-01-25T02:20:42.816", "h"); 27 | assert.strictEqual(d, "2"); 28 | done(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/test_mask-t.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 't'", function () { 5 | it("should format '1876-03-22T23:08:02.429' as 'p'", function (done) { 6 | var date = new Date("1876-03-22T23:08:02.429"); 7 | var d = dateFormat(date, "t"); 8 | assert.strictEqual(d, "p"); 9 | done(); 10 | }); 11 | 12 | it("should format '2013-12-11T05:34:35.350' as 'a'", function (done) { 13 | var date = new Date("2013-12-11T05:34:35.350"); 14 | var d = dateFormat(date, "t"); 15 | assert.strictEqual(d, "a"); 16 | done(); 17 | }); 18 | 19 | it("should format '2020-08-29T00:32:00.101' as 'a'", function (done) { 20 | var d = dateFormat("2020-08-29T00:32:00.101", "t"); 21 | assert.strictEqual(d, "a"); 22 | done(); 23 | }); 24 | 25 | it("should format '2020-09-22T23:04:09.358' as 'p'", function (done) { 26 | var d = dateFormat("2020-09-22T23:04:09.358", "t"); 27 | assert.strictEqual(d, "p"); 28 | done(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/test_mask-t_.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'T'", function () { 5 | it("should format '1654-04-11T08:47:34.086' as 'A'", function (done) { 6 | var date = new Date("1654-04-11T08:47:34.086"); 7 | var d = dateFormat(date, "T"); 8 | assert.strictEqual(d, "A"); 9 | done(); 10 | }); 11 | 12 | it("should format '2001-02-06T15:10:43.798' as 'P'", function (done) { 13 | var date = new Date("2001-02-06T15:10:43.798"); 14 | var d = dateFormat(date, "T"); 15 | assert.strictEqual(d, "P"); 16 | done(); 17 | }); 18 | 19 | it("should format '1998-12-01T12:43:14.920' as 'A'", function (done) { 20 | var d = dateFormat("2020-08-29T00:32:00.101", "T"); 21 | assert.strictEqual(d, "A"); 22 | done(); 23 | }); 24 | 25 | it("should format '2020-10-01T17:20:03.223' as 'p'", function (done) { 26 | var d = dateFormat("2020-10-01T17:20:03.223", "T"); 27 | assert.strictEqual(d, "P"); 28 | done(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/test_mask-h_.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'H'", function () { 5 | it("should format '1883-03-22T07:35:26.419' as '7'", function (done) { 6 | var date = new Date("1883-03-22T07:35:26.419"); 7 | var d = dateFormat(date, "H"); 8 | assert.strictEqual(d, "7"); 9 | done(); 10 | }); 11 | 12 | it("should format '2012-11-07T14:39:48.988' as '14'", function (done) { 13 | var date = new Date("2012-11-07T14:39:48.988"); 14 | var d = dateFormat(date, "H"); 15 | assert.strictEqual(d, "14"); 16 | done(); 17 | }); 18 | 19 | it("should format '1882-01-16T19:37:45.965' as '19'", function (done) { 20 | var d = dateFormat("1882-01-16T19:37:45.965", "H"); 21 | assert.strictEqual(d, "19"); 22 | done(); 23 | }); 24 | 25 | it("should format '2020-08-29T11:20:47.128' as '11'", function (done) { 26 | var d = dateFormat("2020-08-29T11:20:47.128", "H"); 27 | assert.strictEqual(d, "11"); 28 | done(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/test_mask-m_.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'M'", function () { 5 | it("should format '1993-02-12T17:36:01.128' as '36'", function (done) { 6 | var date = new Date("1993-02-12T17:36:01.128"); 7 | var d = dateFormat(date, "M"); 8 | assert.strictEqual(d, "36"); 9 | done(); 10 | }); 11 | 12 | it("should format '2013-11-02T07:00:54.270' as '0'", function (done) { 13 | var date = new Date("2013-11-02T07:00:54.270"); 14 | var d = dateFormat(date, "M"); 15 | assert.strictEqual(d, "0"); 16 | done(); 17 | }); 18 | 19 | it("should format '1873-01-04T11:11:34.700' as '11'", function (done) { 20 | var d = dateFormat("1873-01-04T11:11:34.700", "M"); 21 | assert.strictEqual(d, "11"); 22 | done(); 23 | }); 24 | 25 | it("should format '1734-12-07T09:05:07.972' as '5'", function (done) { 26 | var d = dateFormat("1734-12-07T09:05:07.972", "M"); 27 | assert.strictEqual(d, "5"); 28 | done(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/test_mask-h_h_.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'HH'", function () { 5 | it("should format '1872-02-01T15:55:16.524' as '15'", function (done) { 6 | var date = new Date("1872-02-01T15:55:16.524"); 7 | var d = dateFormat(date, "HH"); 8 | assert.strictEqual(d, "15"); 9 | done(); 10 | }); 11 | 12 | it("should format '2020-10-08T14:32:24.438' as '14'", function (done) { 13 | var date = new Date("2020-10-08T14:32:24.438"); 14 | var d = dateFormat(date, "HH"); 15 | assert.strictEqual(d, "14"); 16 | done(); 17 | }); 18 | 19 | it("should format '2077-12-24T04:20:55.795' as '04'", function (done) { 20 | var d = dateFormat("2077-12-24T04:20:55.795", "HH"); 21 | assert.strictEqual(d, "04"); 22 | done(); 23 | }); 24 | 25 | it("should format '1782-02-11T01:09:41.403' as '01'", function (done) { 26 | var d = dateFormat("1782-02-11T01:09:41.403", "HH"); 27 | assert.strictEqual(d, "01"); 28 | done(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/test_mask-hh.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'hh'", function () { 5 | it("should format '1987-02-11T11:03:16.637' as '11'", function (done) { 6 | var date = new Date("1987-02-11T11:03:16.637"); 7 | var d = dateFormat(date, "hh"); 8 | assert.strictEqual(d, "11"); 9 | done(); 10 | }); 11 | 12 | it("should format '2014-09-28T04:29:52.509' as '04'", function (done) { 13 | var date = new Date("2020-09-28T04:29:52.509"); 14 | var d = dateFormat(date, "hh"); 15 | assert.strictEqual(d, "04"); 16 | done(); 17 | }); 18 | 19 | it("should format '2001-08-02T19:14:19.263' as '07'", function (done) { 20 | var d = dateFormat("2001-08-02T19:14:19.263", "hh"); 21 | assert.strictEqual(d, "07"); 22 | done(); 23 | }); 24 | 25 | it("should format '1872-01-22T19:26:01.744' as '07'", function (done) { 26 | var d = dateFormat("1872-01-22T19:26:01.744", "hh"); 27 | assert.strictEqual(d, "07"); 28 | done(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/test_mask-l.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'l'", function () { 5 | it("should format '2020-10-10T08:48:02.436' as '436'", function (done) { 6 | var date = new Date("2020-10-10T08:48:02.436"); 7 | var d = dateFormat(date, "l"); 8 | assert.strictEqual(d, "436"); 9 | done(); 10 | }); 11 | 12 | it("should format '1993-02-16T14:22:12.654' as '654'", function (done) { 13 | var date = new Date("1993-02-16T14:22:12.654"); 14 | var d = dateFormat(date, "l"); 15 | assert.strictEqual(d, "654"); 16 | done(); 17 | }); 18 | 19 | it("should format '2076-01-03T18:23:30.064' as '064'", function (done) { 20 | var d = dateFormat("2076-01-03T18:23:30.064", "l"); 21 | assert.strictEqual(d, "064"); 22 | done(); 23 | }); 24 | 25 | it("should format '2002-12-25T19:35:55.655' as '655'", function (done) { 26 | var d = dateFormat("2002-12-25T19:35:55.655", "l"); 27 | assert.strictEqual(d, "655"); 28 | done(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/test_mask-m_m_.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'MM'", function () { 5 | it("should format '1876-07-11T13:19:36.341' as '19'", function (done) { 6 | var date = new Date("1876-07-11T13:19:36.341"); 7 | var d = dateFormat(date, "MM"); 8 | assert.strictEqual(d, "19"); 9 | done(); 10 | }); 11 | 12 | it("should format '2013-01-23T07:08:07.942' as '08'", function (done) { 13 | var date = new Date("2013-01-23T07:08:07.942"); 14 | var d = dateFormat(date, "MM"); 15 | assert.strictEqual(d, "08"); 16 | done(); 17 | }); 18 | 19 | it("should format '1982-12-03T08:04:07.203' as '04'", function (done) { 20 | var d = dateFormat("1982-12-03T08:04:07.203", "MM"); 21 | assert.strictEqual(d, "04"); 22 | done(); 23 | }); 24 | 25 | it("should format '2063-09-03T02:38:08.815' as '38'", function (done) { 26 | var d = dateFormat("2063-09-03T02:38:08.815", "MM"); 27 | assert.strictEqual(d, "38"); 28 | done(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/test_mask-ss.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'ss'", function () { 5 | it("should format '1876-03-22T23:08:02.429' as '02'", function (done) { 6 | var date = new Date("1876-03-22T23:08:02.429"); 7 | var d = dateFormat(date, "ss"); 8 | assert.strictEqual(d, "02"); 9 | done(); 10 | }); 11 | 12 | it("should format '2013-12-11T05:34:35.350' as '35'", function (done) { 13 | var date = new Date("2013-12-11T05:34:35.350"); 14 | var d = dateFormat(date, "ss"); 15 | assert.strictEqual(d, "35"); 16 | done(); 17 | }); 18 | 19 | it("should format '2020-08-29T00:32:00.101' as '00'", function (done) { 20 | var d = dateFormat("2020-08-29T00:32:00.101", "ss"); 21 | assert.strictEqual(d, "00"); 22 | done(); 23 | }); 24 | 25 | it("should format '2020-09-22T07:04:09.358' as '09'", function (done) { 26 | var d = dateFormat("2020-09-22T07:04:09.358", "ss"); 27 | assert.strictEqual(d, "09"); 28 | done(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/test_mask-t_t_.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'TT'", function () { 5 | it("should format '1876-04-23T07:35:20.830' as 'AM'", function (done) { 6 | var date = new Date("1876-04-23T07:35:20.830"); 7 | var d = dateFormat(date, "TT"); 8 | assert.strictEqual(d, "AM"); 9 | done(); 10 | }); 11 | 12 | it("should format '2018-04-27T18:50:35.567' as 'PM'", function (done) { 13 | var date = new Date("2018-04-27T18:50:35.567"); 14 | var d = dateFormat(date, "TT"); 15 | assert.strictEqual(d, "PM"); 16 | done(); 17 | }); 18 | 19 | it("should format '2032-05-07T06:45:41.382' as 'AM'", function (done) { 20 | var d = dateFormat("2032-05-07T06:45:41.382", "TT"); 21 | assert.strictEqual(d, "AM"); 22 | done(); 23 | }); 24 | 25 | it("should format '1976-11-25T19:44:08.918' as 'PM'", function (done) { 26 | var d = dateFormat("1976-11-25T19:44:08.918", "TT"); 27 | assert.strictEqual(d, "PM"); 28 | done(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/test_mask-tt.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'tt'", function () { 5 | it("should format '1876-03-22T23:08:02.429' as 'pm'", function (done) { 6 | var date = new Date("1876-03-22T23:08:02.429"); 7 | var d = dateFormat(date, "tt"); 8 | assert.strictEqual(d, "pm"); 9 | done(); 10 | }); 11 | 12 | it("should format '2013-12-11T05:34:35.350' as 'am'", function (done) { 13 | var date = new Date("2013-12-11T05:34:35.350"); 14 | var d = dateFormat(date, "tt"); 15 | assert.strictEqual(d, "am"); 16 | done(); 17 | }); 18 | 19 | it("should format '2020-08-29T00:32:00.101' as 'am'", function (done) { 20 | var d = dateFormat("2020-08-29T00:32:00.101", "tt"); 21 | assert.strictEqual(d, "am"); 22 | done(); 23 | }); 24 | 25 | it("should format '2020-09-22T23:04:09.358' as 'pm'", function (done) { 26 | var d = dateFormat("2020-09-22T23:04:09.358", "tt"); 27 | assert.strictEqual(d, "pm"); 28 | done(); 29 | }); 30 | }); 31 | -------------------------------------------------------------------------------- /test/test_mask-n_.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'N'", function () { 5 | it("should format '1984-02-7' as '2'", function (done) { 6 | var date = new Date("1984-02-7"); 7 | var d = dateFormat(date, "N"); 8 | assert.strictEqual(d, "2"); 9 | done(); 10 | }); 11 | 12 | it("should format '2013-01-17' as '4'", function (done) { 13 | var date = new Date("2013-01-17"); 14 | var d = dateFormat(date, "N"); 15 | assert.strictEqual(d, "4"); 16 | done(); 17 | }); 18 | 19 | it("should format '2034-11-24' as '5'", function (done) { 20 | var d = dateFormat("2034-11-24", "N"); 21 | assert.strictEqual(d, "5"); 22 | done(); 23 | }); 24 | 25 | it("should format '2002-02-3' as '7'", function (done) { 26 | var d = dateFormat("2002-02-3", "N"); 27 | assert.strictEqual(d, "7"); 28 | done(); 29 | }); 30 | 31 | it("should format '2002-02-4' as '1'", function (done) { 32 | var d = dateFormat("2002-02-4", "N"); 33 | assert.strictEqual(d, "1"); 34 | done(); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | (c) 2007-2009 Steven Levithan 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /test/test_mask-l_.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'L'", function () { 5 | it("should format '2020-10-10T08:48:02.436' as '44'", function (done) { 6 | var date = new Date("2020-10-10T08:48:02.436"); 7 | var d = dateFormat(date, "L"); 8 | assert.strictEqual(d, "43"); 9 | done(); 10 | }); 11 | 12 | it("should format '1993-02-16T14:22:12.654' as '65'", function (done) { 13 | var date = new Date("1993-02-16T14:22:12.654"); 14 | var d = dateFormat(date, "L"); 15 | assert.strictEqual(d, "65"); 16 | done(); 17 | }); 18 | 19 | it("should format '2076-01-03T18:23:30.064' as '06'", function (done) { 20 | var d = dateFormat("2076-01-03T18:23:30.064", "L"); 21 | assert.strictEqual(d, "06"); 22 | done(); 23 | }); 24 | 25 | it("should format '2002-12-25T19:35:55.655' as '66'", function (done) { 26 | var d = dateFormat("2002-12-25T19:35:55.655", "L"); 27 | assert.strictEqual(d, "65"); 28 | done(); 29 | }); 30 | 31 | it("should format '2126-07-23T03:15:25.999' as '99'", function (done) { 32 | var d = dateFormat("2126-07-23T03:15:25.999", "L"); 33 | assert.strictEqual(d, "99"); 34 | done(); 35 | }); 36 | }); 37 | -------------------------------------------------------------------------------- /test/test_mask-yyyy.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | var dateFormat = require("../lib/dateformat"); 3 | 4 | describe("Mask: 'yyyy'", function () { 5 | it("should format '1992-10-6' as '1992'", function (done) { 6 | var date = new Date("1992-10-6"); 7 | var d = dateFormat(date, "yyyy"); 8 | assert.strictEqual(d, "1992"); 9 | done(); 10 | }); 11 | 12 | it("should format '2078-02-11' as '2078'", function (done) { 13 | var date = new Date("2078-02-11"); 14 | var d = dateFormat(date, "yyyy"); 15 | assert.strictEqual(d, "2078"); 16 | done(); 17 | }); 18 | 19 | it("should format '1763-12-02' as '1763'", function (done) { 20 | var date = new Date("1763-12-02"); 21 | var d = dateFormat(date, "yyyy"); 22 | assert.strictEqual(d, "1763"); 23 | done(); 24 | }); 25 | 26 | it("should format '0999-01-01' as '0999'", function (done) { 27 | var date = new Date("0999-01-01"); 28 | var d = dateFormat(date, "yyyy"); 29 | assert.strictEqual(d, "0999"); 30 | done(); 31 | }); 32 | 33 | it("should format '0002-12-11' as '0002'", function (done) { 34 | var date = new Date("0002-12-11"); 35 | var d = dateFormat(date, "yyyy"); 36 | assert.strictEqual(d, "0002"); 37 | done(); 38 | }); 39 | }); 40 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to dateformat 2 | 3 | Thanks for taking the time to contribute to dateformat!!! 4 | Feel free to raise a PR or Issue for any suggested improvements to the package. 5 | 6 | ## Running dateformat 7 | 8 | To run dateformat, first clone the repository to your local machine. 9 | Then run `npm i` to install all the required dependencies. 10 | Then you can import dateformat from the `lib` directory and call it as needed. 11 | 12 | ## Benchmarking 13 | 14 | If you want to try and improve the performance of dateformat, you can use the benchmarking script. 15 | To use the script: 16 | 17 | - Copy the contents of the `lib/dateformat.js` file into the `benchmark/previousDateFormat.js` file. 18 | - Make your code changes to `lib/dateformat.js` as you normaly would 19 | - run `npm run benchmark` in root directory to see the comparison of performance before and after your change 20 | 21 | A positive number for improvement indicates that your changes have improved the speed of dateformat. 22 | Any different between -5% and 5% can be disregarded as minor variance. 23 | It is recommended that you run this benchmark after any changes to the business logic of dateformat. 24 | This is to ensure that there is no detrimental impact on performance from your change. 25 | A screenshot it the PR would be much appreciated also. 26 | 27 | ## Running Tests 28 | 29 | To test dateformat, just run `npm run test` and see the results. 30 | -------------------------------------------------------------------------------- /test/test_formats.js: -------------------------------------------------------------------------------- 1 | var assert = require("assert"); 2 | 3 | var dateFormat = require("../lib/dateformat"); 4 | 5 | var expects = { 6 | default: "Sat Mar 08 2014 13:19:44", 7 | shortDate: "3/8/14", 8 | paddedShortDate: "03/08/2014", 9 | mediumDate: "Mar 8, 2014", 10 | longDate: "March 8, 2014", 11 | fullDate: "Saturday, March 8, 2014", 12 | shortTime: "1:19 PM", 13 | mediumTime: "1:19:44 PM", 14 | longTime: "1:19:44 PM %TZ_PREFIX%%TZ_OFFSET%", 15 | isoDate: "2014-03-08", 16 | isoTime: "13:19:44", 17 | isoDateTime: "2014-03-08T13:19:44%TZ_OFFSET%", 18 | isoUtcDateTime: "", 19 | expiresHeaderFormat: "Sat, 08 Mar 2014 13:19:44 %TZ_PREFIX%%TZ_OFFSET%", 20 | }; 21 | 22 | function pad(num, size) { 23 | var s = num + ""; 24 | while (s.length < size) { 25 | s = "0" + s; 26 | } 27 | return s; 28 | } 29 | 30 | function parseOffset(date) { 31 | var offset = date.getTimezoneOffset(); 32 | var hours = Math.floor((-1 * offset) / 60); 33 | var minutes = -1 * offset - hours * 60; 34 | var sign = offset > 0 ? "-" : "+"; 35 | return { 36 | offset: offset, 37 | hours: hours, 38 | minutes: minutes, 39 | sign: sign, 40 | }; 41 | } 42 | 43 | function timezoneOffset(date) { 44 | var offset = parseOffset(date); 45 | return offset.sign + pad(offset.hours, 2) + pad(offset.minutes, 2); 46 | } 47 | 48 | describe("dateformat([now], [mask])", function () { 49 | Object.keys(dateFormat.masks).forEach(function (key) { 50 | it("should format `" + key + "` mask", function (done) { 51 | var now = new Date(2014, 2, 8, 13, 19, 44); 52 | var tzOffset = timezoneOffset(now); 53 | var expected = expects[key] 54 | .replace(/%TZ_PREFIX%/, "GMT") 55 | .replace(/%TZ_OFFSET%/g, tzOffset) 56 | .replace(/GMT\+0000/g, "UTC"); 57 | if (key === "isoUtcDateTime") { 58 | var offset = parseOffset(now); 59 | now.setHours( 60 | now.getHours() - offset.hours, 61 | now.getMinutes() - offset.minutes 62 | ); 63 | var expected = now.toISOString().replace(/\.000/g, ""); 64 | } 65 | var actual = dateFormat(now, key); 66 | assert.strictEqual(actual, expected); 67 | done(); 68 | }); 69 | }); 70 | it("should use `default` mask, when `mask` is empty", function (done) { 71 | var now = new Date(2014, 2, 8, 13, 19, 44); 72 | var expected = expects["default"]; 73 | var actual = dateFormat(now); 74 | 75 | assert.strictEqual(actual, expected); 76 | done(); 77 | }); 78 | }); 79 | -------------------------------------------------------------------------------- /test/test_threedays.js: -------------------------------------------------------------------------------- 1 | const assert = require('assert'); 2 | 3 | const dateFormat = require('./../lib/dateformat'); 4 | 5 | const dayNamesShort = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; 6 | const dayNamesLong = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; 7 | const threeDays = ['Yesterday', 'Today', 'Tomorrow', 'Ysd', 'Tdy', 'Tmw']; 8 | 9 | describe('threeDays', function () { 10 | let date, DDD, DDDD; 11 | beforeEach(function () { 12 | date = new Date(); 13 | }); 14 | it('should return "Yesterday" (Today - 1 day)', function (done) { 15 | date.setDate(date.getDate() - 1); 16 | DDDD = dateFormat(date, 'DDDD'); 17 | assert.strictEqual(DDDD, "Yesterday"); 18 | done(); 19 | }); 20 | it('should return "Ysd" (Today - 1 day)', function (done) { 21 | date.setDate(date.getDate() - 1); 22 | DDD = dateFormat(date, 'DDD'); 23 | assert.strictEqual(DDD, "Ysd"); 24 | done(); 25 | }); 26 | it('should return "Today" (Today)', function (done) { 27 | DDDD = dateFormat(date, 'DDDD'); 28 | assert.strictEqual(DDDD, "Today"); 29 | done(); 30 | }); 31 | it('should return "Tdy" (Today)', function (done) { 32 | DDD = dateFormat(date, 'DDD'); 33 | assert.strictEqual(DDD, "Tdy"); 34 | done(); 35 | }); 36 | it('should return "Tomorrow" (Today + 1 day)', function (done) { 37 | date.setDate(date.getDate() + 1); 38 | DDDD = dateFormat(date, 'DDDD'); 39 | assert.strictEqual(DDDD, "Tomorrow"); 40 | done(); 41 | }); 42 | it('should return "Tmw" (Today + 1 day)', function (done) { 43 | date.setDate(date.getDate() + 1); 44 | DDD = dateFormat(date, 'DDD'); 45 | assert.strictEqual(DDD, "Tmw"); 46 | done(); 47 | }); 48 | it('should not return "Yesterday", "Today", "Tomorrow", "Ysd", "Tdy", or "Tmw" (Today - 2 days)', function (done) { 49 | date.setDate(date.getDate() - 2); 50 | DDD = dateFormat(date, 'DDD'); 51 | DDDD = dateFormat(date, 'DDDD'); 52 | assert.strictEqual(threeDays.indexOf(DDD), -1); 53 | assert.strictEqual(threeDays.indexOf(DDDD), -1); 54 | done(); 55 | }); 56 | it('should not return "Yesterday", "Today" or "Tomorrow", "Ysd", "Tdy", or "Tmw" (Today + 2 days)', function (done) { 57 | date.setDate(date.getDate() + 2); 58 | DDD = dateFormat(date, 'DDD'); 59 | DDDD = dateFormat(date, 'DDDD'); 60 | assert.strictEqual(threeDays.indexOf(DDD), -1); 61 | assert.strictEqual(threeDays.indexOf(DDDD), -1); 62 | done(); 63 | }); 64 | it('should return short day name (Today - 2 days)', function (done) { 65 | date.setDate(date.getDate() - 2); 66 | DDD = dateFormat(date, 'DDD'); 67 | assert.notStrictEqual(dayNamesShort.indexOf(DDD), -1); 68 | done(); 69 | }); 70 | it('should return short day name (Today + 2 days)', function (done) { 71 | date.setDate(date.getDate() + 2); 72 | DDD = dateFormat(date, 'DDD'); 73 | assert.notStrictEqual(dayNamesShort.indexOf(DDD), -1); 74 | done(); 75 | }); 76 | it('should return long day name (Today - 2 days)', function (done) { 77 | date.setDate(date.getDate() - 2); 78 | DDDD = dateFormat(date, 'DDDD'); 79 | assert.notStrictEqual(dayNamesLong.indexOf(DDDD), -1); 80 | done(); 81 | }); 82 | it('should return short day name (Today + 2 days)', function (done) { 83 | date.setDate(date.getDate() + 2); 84 | DDDD = dateFormat(date, 'DDDD'); 85 | assert.notStrictEqual(dayNamesLong.indexOf(DDDD), -1); 86 | done(); 87 | }); 88 | }); 89 | -------------------------------------------------------------------------------- /lib/dateformat.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Date Format 1.2.3 3 | * (c) 2007-2009 Steven Levithan 4 | * MIT license 5 | * 6 | * Includes enhancements by Scott Trenda 7 | * and Kris Kowal 8 | * 9 | * Accepts a date, a mask, or a date and a mask. 10 | * Returns a formatted version of the given date. 11 | * The date defaults to the current date/time. 12 | * The mask defaults to dateFormat.masks.default. 13 | */ 14 | 15 | (function (global) { 16 | const dateFormat = (() => { 17 | const token = /d{1,4}|D{3,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|W{1,2}|[LlopSZN]|"[^"]*"|'[^']*'/g; 18 | const timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g; 19 | const timezoneClip = /[^-+\dA-Z]/g; 20 | 21 | // Regexes and supporting functions are cached through closure 22 | return (date, mask, utc, gmt) => { 23 | // You can't provide utc if you skip other args (use the 'UTC:' mask prefix) 24 | if ( 25 | arguments.length === 1 && 26 | kindOf(date) === "string" && 27 | !/\d/.test(date) 28 | ) { 29 | mask = date; 30 | date = undefined; 31 | } 32 | 33 | date = date || date === 0 ? date : new Date(); 34 | 35 | if (!(date instanceof Date)) { 36 | date = new Date(date); 37 | } 38 | 39 | if (isNaN(date)) { 40 | throw TypeError("Invalid date"); 41 | } 42 | 43 | mask = String( 44 | dateFormat.masks[mask] || mask || dateFormat.masks["default"] 45 | ); 46 | 47 | // Allow setting the utc/gmt argument via the mask 48 | const maskSlice = mask.slice(0, 4); 49 | if (maskSlice === "UTC:" || maskSlice === "GMT:") { 50 | mask = mask.slice(4); 51 | utc = true; 52 | if (maskSlice === "GMT:") { 53 | gmt = true; 54 | } 55 | } 56 | 57 | const _ = () => (utc ? "getUTC" : "get"); 58 | const d = () => date[_() + "Date"](); 59 | const D = () => date[_() + "Day"](); 60 | const m = () => date[_() + "Month"](); 61 | const y = () => date[_() + "FullYear"](); 62 | const H = () => date[_() + "Hours"](); 63 | const M = () => date[_() + "Minutes"](); 64 | const s = () => date[_() + "Seconds"](); 65 | const L = () => date[_() + "Milliseconds"](); 66 | const o = () => (utc ? 0 : date.getTimezoneOffset()); 67 | const W = () => getWeek(date); 68 | const N = () => getDayOfWeek(date); 69 | 70 | const flags = { 71 | d: () => d(), 72 | dd: () => pad(d()), 73 | ddd: () => dateFormat.i18n.dayNames[D()], 74 | DDD: () => getDayName({ 75 | y: y(), 76 | m: m(), 77 | d: d(), 78 | _: _(), 79 | dayName: dateFormat.i18n.dayNames[D()], 80 | short: true 81 | }), 82 | dddd: () => dateFormat.i18n.dayNames[D() + 7], 83 | DDDD: () => getDayName({ 84 | y: y(), 85 | m: m(), 86 | d: d(), 87 | _: _(), 88 | dayName: dateFormat.i18n.dayNames[D() + 7] 89 | }), 90 | m: () => m() + 1, 91 | mm: () => pad(m() + 1), 92 | mmm: () => dateFormat.i18n.monthNames[m()], 93 | mmmm: () => dateFormat.i18n.monthNames[m() + 12], 94 | yy: () => String(y()).slice(2), 95 | yyyy: () => pad(y(), 4), 96 | h: () => H() % 12 || 12, 97 | hh: () => pad(H() % 12 || 12), 98 | H: () => H(), 99 | HH: () => pad(H()), 100 | M: () => M(), 101 | MM: () => pad(M()), 102 | s: () => s(), 103 | ss: () => pad(s()), 104 | l: () => pad(L(), 3), 105 | L: () => pad(Math.floor(L() / 10)), 106 | t: () => 107 | H() < 12 108 | ? dateFormat.i18n.timeNames[0] 109 | : dateFormat.i18n.timeNames[1], 110 | tt: () => 111 | H() < 12 112 | ? dateFormat.i18n.timeNames[2] 113 | : dateFormat.i18n.timeNames[3], 114 | T: () => 115 | H() < 12 116 | ? dateFormat.i18n.timeNames[4] 117 | : dateFormat.i18n.timeNames[5], 118 | TT: () => 119 | H() < 12 120 | ? dateFormat.i18n.timeNames[6] 121 | : dateFormat.i18n.timeNames[7], 122 | Z: () => 123 | gmt 124 | ? "GMT" 125 | : utc 126 | ? "UTC" 127 | : (String(date).match(timezone) || [""]) 128 | .pop() 129 | .replace(timezoneClip, "") 130 | .replace(/GMT\+0000/g, "UTC"), 131 | o: () => 132 | (o() > 0 ? "-" : "+") + 133 | pad(Math.floor(Math.abs(o()) / 60) * 100 + (Math.abs(o()) % 60), 4), 134 | p: () => 135 | (o() > 0 ? "-" : "+") + 136 | pad(Math.floor(Math.abs(o()) / 60), 2) + 137 | ":" + 138 | pad(Math.floor(Math.abs(o()) % 60), 2), 139 | S: () => 140 | ["th", "st", "nd", "rd"][ 141 | d() % 10 > 3 ? 0 : (((d() % 100) - (d() % 10) != 10) * d()) % 10 142 | ], 143 | W: () => W(), 144 | WW: () => pad( W() ), 145 | N: () => N(), 146 | }; 147 | 148 | return mask.replace(token, (match) => { 149 | if (match in flags) { 150 | return flags[match](); 151 | } 152 | return match.slice(1, match.length - 1); 153 | }); 154 | }; 155 | })(); 156 | 157 | dateFormat.masks = { 158 | default: "ddd mmm dd yyyy HH:MM:ss", 159 | shortDate: "m/d/yy", 160 | paddedShortDate: "mm/dd/yyyy", 161 | mediumDate: "mmm d, yyyy", 162 | longDate: "mmmm d, yyyy", 163 | fullDate: "dddd, mmmm d, yyyy", 164 | shortTime: "h:MM TT", 165 | mediumTime: "h:MM:ss TT", 166 | longTime: "h:MM:ss TT Z", 167 | isoDate: "yyyy-mm-dd", 168 | isoTime: "HH:MM:ss", 169 | isoDateTime: "yyyy-mm-dd'T'HH:MM:sso", 170 | isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'", 171 | expiresHeaderFormat: "ddd, dd mmm yyyy HH:MM:ss Z", 172 | }; 173 | 174 | // Internationalization strings 175 | dateFormat.i18n = { 176 | dayNames: [ 177 | "Sun", 178 | "Mon", 179 | "Tue", 180 | "Wed", 181 | "Thu", 182 | "Fri", 183 | "Sat", 184 | "Sunday", 185 | "Monday", 186 | "Tuesday", 187 | "Wednesday", 188 | "Thursday", 189 | "Friday", 190 | "Saturday", 191 | ], 192 | monthNames: [ 193 | "Jan", 194 | "Feb", 195 | "Mar", 196 | "Apr", 197 | "May", 198 | "Jun", 199 | "Jul", 200 | "Aug", 201 | "Sep", 202 | "Oct", 203 | "Nov", 204 | "Dec", 205 | "January", 206 | "February", 207 | "March", 208 | "April", 209 | "May", 210 | "June", 211 | "July", 212 | "August", 213 | "September", 214 | "October", 215 | "November", 216 | "December", 217 | ], 218 | timeNames: ["a", "p", "am", "pm", "A", "P", "AM", "PM"], 219 | }; 220 | 221 | const pad = (val, len) => { 222 | val = String(val); 223 | len = len || 2; 224 | while (val.length < len) { 225 | val = "0" + val; 226 | } 227 | return val; 228 | }; 229 | 230 | /** 231 | * Get day name 232 | * Yesterday, Today, Tomorrow if the date lies within, else fallback to Monday - Sunday 233 | * @param {Object} 234 | * @return {String} 235 | */ 236 | const getDayName = ({ y, m, d, _, dayName, short = false }) => { 237 | const today = new Date(); 238 | const yesterday = new Date(); 239 | yesterday.setDate(yesterday[_ + 'Date']() - 1); 240 | const tomorrow = new Date(); 241 | tomorrow.setDate(tomorrow[_ + 'Date']() + 1); 242 | const today_d = () => today[_ + 'Date'](); 243 | const today_m = () => today[_ + 'Month'](); 244 | const today_y = () => today[_ + 'FullYear'](); 245 | const yesterday_d = () => yesterday[_ + 'Date'](); 246 | const yesterday_m = () => yesterday[_ + 'Month'](); 247 | const yesterday_y = () => yesterday[_ + 'FullYear'](); 248 | const tomorrow_d = () => tomorrow[_ + 'Date'](); 249 | const tomorrow_m = () => tomorrow[_ + 'Month'](); 250 | const tomorrow_y = () => tomorrow[_ + 'FullYear'](); 251 | 252 | if (today_y() === y && today_m() === m && today_d() === d) { 253 | return short ? 'Tdy' : 'Today'; 254 | } 255 | else if (yesterday_y() === y && yesterday_m() === m && yesterday_d() === d) { 256 | return short ? 'Ysd' : 'Yesterday'; 257 | } 258 | else if (tomorrow_y() === y && tomorrow_m() === m && tomorrow_d() === d) { 259 | return short ? 'Tmw' : 'Tomorrow'; 260 | } 261 | return dayName; 262 | }; 263 | 264 | /** 265 | * Get the ISO 8601 week number 266 | * Based on comments from 267 | * http://techblog.procurios.nl/k/n618/news/view/33796/14863/Calculate-ISO-8601-week-and-year-in-javascript.html 268 | * 269 | * @param {Object} `date` 270 | * @return {Number} 271 | */ 272 | const getWeek = (date) => { 273 | // Remove time components of date 274 | const targetThursday = new Date( 275 | date.getFullYear(), 276 | date.getMonth(), 277 | date.getDate() 278 | ); 279 | 280 | // Change date to Thursday same week 281 | targetThursday.setDate( 282 | targetThursday.getDate() - ((targetThursday.getDay() + 6) % 7) + 3 283 | ); 284 | 285 | // Take January 4th as it is always in week 1 (see ISO 8601) 286 | const firstThursday = new Date(targetThursday.getFullYear(), 0, 4); 287 | 288 | // Change date to Thursday same week 289 | firstThursday.setDate( 290 | firstThursday.getDate() - ((firstThursday.getDay() + 6) % 7) + 3 291 | ); 292 | 293 | // Check if daylight-saving-time-switch occurred and correct for it 294 | const ds = 295 | targetThursday.getTimezoneOffset() - firstThursday.getTimezoneOffset(); 296 | targetThursday.setHours(targetThursday.getHours() - ds); 297 | 298 | // Number of weeks between target Thursday and first Thursday 299 | const weekDiff = (targetThursday - firstThursday) / (86400000 * 7); 300 | return 1 + Math.floor(weekDiff); 301 | }; 302 | 303 | /** 304 | * Get ISO-8601 numeric representation of the day of the week 305 | * 1 (for Monday) through 7 (for Sunday) 306 | * 307 | * @param {Object} `date` 308 | * @return {Number} 309 | */ 310 | const getDayOfWeek = (date) => { 311 | let dow = date.getDay(); 312 | if (dow === 0) { 313 | dow = 7; 314 | } 315 | return dow; 316 | }; 317 | 318 | /** 319 | * kind-of shortcut 320 | * @param {*} val 321 | * @return {String} 322 | */ 323 | const kindOf = (val) => { 324 | if (val === null) { 325 | return "null"; 326 | } 327 | 328 | if (val === undefined) { 329 | return "undefined"; 330 | } 331 | 332 | if (typeof val !== "object") { 333 | return typeof val; 334 | } 335 | 336 | if (Array.isArray(val)) { 337 | return "array"; 338 | } 339 | 340 | return {}.toString.call(val).slice(8, -1).toLowerCase(); 341 | }; 342 | 343 | if (typeof define === "function" && define.amd) { 344 | define(() => { 345 | return dateFormat; 346 | }); 347 | } else if (typeof exports === "object") { 348 | module.exports = dateFormat; 349 | } else { 350 | global.dateFormat = dateFormat; 351 | } 352 | })(this); 353 | -------------------------------------------------------------------------------- /benchmark/previousDateFormat.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function _typeof(obj) { 4 | "@babel/helpers - typeof"; 5 | if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { 6 | _typeof = function _typeof(obj) { 7 | return typeof obj; 8 | }; 9 | } else { 10 | _typeof = function _typeof(obj) { 11 | return obj && 12 | typeof Symbol === "function" && 13 | obj.constructor === Symbol && 14 | obj !== Symbol.prototype 15 | ? "symbol" 16 | : typeof obj; 17 | }; 18 | } 19 | return _typeof(obj); 20 | } 21 | 22 | /* 23 | * Date Format 1.2.3 24 | * (c) 2007-2009 Steven Levithan 25 | * MIT license 26 | * 27 | * Includes enhancements by Scott Trenda 28 | * and Kris Kowal 29 | * 30 | * Accepts a date, a mask, or a date and a mask. 31 | * Returns a formatted version of the given date. 32 | * The date defaults to the current date/time. 33 | * The mask defaults to dateFormat.masks.default. 34 | */ 35 | (function (global) { 36 | "use strict"; 37 | 38 | var dateFormat = (function () { 39 | var token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LlopSZWN]|"[^"]*"|'[^']*'/g; 40 | var timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g; 41 | var timezoneClip = /[^-+\dA-Z]/g; // Regexes and supporting functions are cached through closure 42 | 43 | return function (date, mask, utc, gmt) { 44 | // You can't provide utc if you skip other args (use the 'UTC:' mask prefix) 45 | if ( 46 | arguments.length === 1 && 47 | kindOf(date) === "string" && 48 | !/\d/.test(date) 49 | ) { 50 | mask = date; 51 | date = undefined; 52 | } 53 | 54 | date = date || new Date(); 55 | 56 | if (!(date instanceof Date)) { 57 | date = new Date(date); 58 | } 59 | 60 | if (isNaN(date)) { 61 | throw TypeError("Invalid date"); 62 | } 63 | 64 | mask = String( 65 | dateFormat.masks[mask] || mask || dateFormat.masks["default"] 66 | ); // Allow setting the utc/gmt argument via the mask 67 | 68 | var maskSlice = mask.slice(0, 4); 69 | 70 | if (maskSlice === "UTC:" || maskSlice === "GMT:") { 71 | mask = mask.slice(4); 72 | utc = true; 73 | 74 | if (maskSlice === "GMT:") { 75 | gmt = true; 76 | } 77 | } 78 | 79 | var _ = function _() { 80 | return utc ? "getUTC" : "get"; 81 | }; 82 | 83 | var _d = function d() { 84 | return date[_() + "Date"](); 85 | }; 86 | 87 | var D = function D() { 88 | return date[_() + "Day"](); 89 | }; 90 | 91 | var _m = function m() { 92 | return date[_() + "Month"](); 93 | }; 94 | 95 | var y = function y() { 96 | return date[_() + "FullYear"](); 97 | }; 98 | 99 | var _H = function H() { 100 | return date[_() + "Hours"](); 101 | }; 102 | 103 | var _M = function M() { 104 | return date[_() + "Minutes"](); 105 | }; 106 | 107 | var _s = function s() { 108 | return date[_() + "Seconds"](); 109 | }; 110 | 111 | var _L = function L() { 112 | return date[_() + "Milliseconds"](); 113 | }; 114 | 115 | var _o = function o() { 116 | return utc ? 0 : date.getTimezoneOffset(); 117 | }; 118 | 119 | var _W = function W() { 120 | return getWeek(date); 121 | }; 122 | 123 | var _N = function N() { 124 | return getDayOfWeek(date); 125 | }; 126 | 127 | var flags = { 128 | d: function d() { 129 | return _d(); 130 | }, 131 | dd: function dd() { 132 | return pad(_d()); 133 | }, 134 | ddd: function ddd() { 135 | return dateFormat.i18n.dayNames[D()]; 136 | }, 137 | dddd: function dddd() { 138 | return dateFormat.i18n.dayNames[D() + 7]; 139 | }, 140 | m: function m() { 141 | return _m() + 1; 142 | }, 143 | mm: function mm() { 144 | return pad(_m() + 1); 145 | }, 146 | mmm: function mmm() { 147 | return dateFormat.i18n.monthNames[_m()]; 148 | }, 149 | mmmm: function mmmm() { 150 | return dateFormat.i18n.monthNames[_m() + 12]; 151 | }, 152 | yy: function yy() { 153 | return String(y()).slice(2); 154 | }, 155 | yyyy: function yyyy() { 156 | return y(); 157 | }, 158 | h: function h() { 159 | return _H() % 12 || 12; 160 | }, 161 | hh: function hh() { 162 | return pad(_H() % 12 || 12); 163 | }, 164 | H: function H() { 165 | return _H(); 166 | }, 167 | HH: function HH() { 168 | return pad(_H()); 169 | }, 170 | M: function M() { 171 | return _M(); 172 | }, 173 | MM: function MM() { 174 | return pad(_M()); 175 | }, 176 | s: function s() { 177 | return _s(); 178 | }, 179 | ss: function ss() { 180 | return pad(_s()); 181 | }, 182 | l: function l() { 183 | return pad(_L(), 3); 184 | }, 185 | L: function L() { 186 | return pad(Math.round(_L() / 10)); 187 | }, 188 | t: function t() { 189 | return _H() < 12 190 | ? dateFormat.i18n.timeNames[0] 191 | : dateFormat.i18n.timeNames[1]; 192 | }, 193 | tt: function tt() { 194 | return _H() < 12 195 | ? dateFormat.i18n.timeNames[2] 196 | : dateFormat.i18n.timeNames[3]; 197 | }, 198 | T: function T() { 199 | return _H() < 12 200 | ? dateFormat.i18n.timeNames[4] 201 | : dateFormat.i18n.timeNames[5]; 202 | }, 203 | TT: function TT() { 204 | return _H() < 12 205 | ? dateFormat.i18n.timeNames[6] 206 | : dateFormat.i18n.timeNames[7]; 207 | }, 208 | Z: function Z() { 209 | return gmt 210 | ? "GMT" 211 | : utc 212 | ? "UTC" 213 | : (String(date).match(timezone) || [""]) 214 | .pop() 215 | .replace(timezoneClip, "") 216 | .replace(/GMT\+0000/g, "UTC"); 217 | }, 218 | o: function o() { 219 | return ( 220 | (_o() > 0 ? "-" : "+") + 221 | pad( 222 | Math.floor(Math.abs(_o()) / 60) * 100 + (Math.abs(_o()) % 60), 223 | 4 224 | ) 225 | ); 226 | }, 227 | p: function p() { 228 | return ( 229 | (_o() > 0 ? "-" : "+") + 230 | pad(Math.floor(Math.abs(_o()) / 60), 2) + 231 | ':' + 232 | pad(Math.floor(Math.abs(_o()) % 60), 2) 233 | ); 234 | }, 235 | S: function S() { 236 | return ["th", "st", "nd", "rd"][ 237 | _d() % 10 > 3 ? 0 : (((_d() % 100) - (_d() % 10) != 10) * _d()) % 10 238 | ]; 239 | }, 240 | W: function W() { 241 | return _W(); 242 | }, 243 | N: function N() { 244 | return _N(); 245 | }, 246 | }; 247 | return mask.replace(token, function (match) { 248 | if (match in flags) { 249 | return flags[match](); 250 | } 251 | 252 | return match.slice(1, match.length - 1); 253 | }); 254 | }; 255 | })(); 256 | 257 | dateFormat.masks = { 258 | default: "ddd mmm dd yyyy HH:MM:ss", 259 | shortDate: "m/d/yy", 260 | mediumDate: "mmm d, yyyy", 261 | longDate: "mmmm d, yyyy", 262 | fullDate: "dddd, mmmm d, yyyy", 263 | shortTime: "h:MM TT", 264 | mediumTime: "h:MM:ss TT", 265 | longTime: "h:MM:ss TT Z", 266 | isoDate: "yyyy-mm-dd", 267 | isoTime: "HH:MM:ss", 268 | isoDateTime: "yyyy-mm-dd'T'HH:MM:sso", 269 | isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'", 270 | expiresHeaderFormat: "ddd, dd mmm yyyy HH:MM:ss Z", 271 | }; // Internationalization strings 272 | 273 | dateFormat.i18n = { 274 | dayNames: [ 275 | "Sun", 276 | "Mon", 277 | "Tue", 278 | "Wed", 279 | "Thu", 280 | "Fri", 281 | "Sat", 282 | "Sunday", 283 | "Monday", 284 | "Tuesday", 285 | "Wednesday", 286 | "Thursday", 287 | "Friday", 288 | "Saturday", 289 | ], 290 | monthNames: [ 291 | "Jan", 292 | "Feb", 293 | "Mar", 294 | "Apr", 295 | "May", 296 | "Jun", 297 | "Jul", 298 | "Aug", 299 | "Sep", 300 | "Oct", 301 | "Nov", 302 | "Dec", 303 | "January", 304 | "February", 305 | "March", 306 | "April", 307 | "May", 308 | "June", 309 | "July", 310 | "August", 311 | "September", 312 | "October", 313 | "November", 314 | "December", 315 | ], 316 | timeNames: ["a", "p", "am", "pm", "A", "P", "AM", "PM"], 317 | }; 318 | 319 | function pad(val, len) { 320 | val = String(val); 321 | len = len || 2; 322 | 323 | while (val.length < len) { 324 | val = "0" + val; 325 | } 326 | 327 | return val; 328 | } 329 | /** 330 | * Get the ISO 8601 week number 331 | * Based on comments from 332 | * http://techblog.procurios.nl/k/n618/news/view/33796/14863/Calculate-ISO-8601-week-and-year-in-javascript.html 333 | * 334 | * @param {Object} `date` 335 | * @return {Number} 336 | */ 337 | 338 | function getWeek(date) { 339 | // Remove time components of date 340 | var targetThursday = new Date( 341 | date.getFullYear(), 342 | date.getMonth(), 343 | date.getDate() 344 | ); // Change date to Thursday same week 345 | 346 | targetThursday.setDate( 347 | targetThursday.getDate() - ((targetThursday.getDay() + 6) % 7) + 3 348 | ); // Take January 4th as it is always in week 1 (see ISO 8601) 349 | 350 | var firstThursday = new Date(targetThursday.getFullYear(), 0, 4); // Change date to Thursday same week 351 | 352 | firstThursday.setDate( 353 | firstThursday.getDate() - ((firstThursday.getDay() + 6) % 7) + 3 354 | ); // Check if daylight-saving-time-switch occurred and correct for it 355 | 356 | var ds = 357 | targetThursday.getTimezoneOffset() - firstThursday.getTimezoneOffset(); 358 | targetThursday.setHours(targetThursday.getHours() - ds); // Number of weeks between target Thursday and first Thursday 359 | 360 | var weekDiff = (targetThursday - firstThursday) / (86400000 * 7); 361 | return 1 + Math.floor(weekDiff); 362 | } 363 | /** 364 | * Get ISO-8601 numeric representation of the day of the week 365 | * 1 (for Monday) through 7 (for Sunday) 366 | * 367 | * @param {Object} `date` 368 | * @return {Number} 369 | */ 370 | 371 | function getDayOfWeek(date) { 372 | var dow = date.getDay(); 373 | 374 | if (dow === 0) { 375 | dow = 7; 376 | } 377 | 378 | return dow; 379 | } 380 | /** 381 | * kind-of shortcut 382 | * @param {*} val 383 | * @return {String} 384 | */ 385 | 386 | function kindOf(val) { 387 | if (val === null) { 388 | return "null"; 389 | } 390 | 391 | if (val === undefined) { 392 | return "undefined"; 393 | } 394 | 395 | if (_typeof(val) !== "object") { 396 | return _typeof(val); 397 | } 398 | 399 | if (Array.isArray(val)) { 400 | return "array"; 401 | } 402 | 403 | return {}.toString.call(val).slice(8, -1).toLowerCase(); 404 | } 405 | 406 | if (typeof define === "function" && define.amd) { 407 | define(function () { 408 | return dateFormat; 409 | }); 410 | } else if ( 411 | (typeof exports === "undefined" ? "undefined" : _typeof(exports)) === 412 | "object" 413 | ) { 414 | module.exports = dateFormat; 415 | } else { 416 | global.dateFormat = dateFormat; 417 | } 418 | })(void 0); 419 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # dateformat 2 | 3 | A node.js package for Steven Levithan's excellent [dateFormat()][dateformat] function. 4 | 5 | This module was forked from https://github.com/felixge/node-dateformat to avoid a CJS to ESM migration. 6 | This fork will be maintained as long as it is necessary. 7 | 8 | ## Modifications 9 | 10 | - Removed the `Date.prototype.format` method. Sorry folks, but extending native prototypes is for suckers. 11 | - Added a `module.exports = dateFormat;` statement at the bottom 12 | - Added the placeholder `N` to get the ISO 8601 numeric representation of the day of the week 13 | 14 | ## Installation 15 | 16 | ```bash 17 | $ npm install @matteo.collina/dateformat 18 | $ dateformat --help 19 | ``` 20 | 21 | ## Usage 22 | 23 | As taken from Steven's post, modified to match the Modifications listed above: 24 | 25 | ```js 26 | var dateFormat = require("@matteo.collina/dateformat"); 27 | var now = new Date(); 28 | 29 | // Basic usage 30 | dateFormat(now, "dddd, mmmm dS, yyyy, h:MM:ss TT"); 31 | // Saturday, June 9th, 2007, 5:46:21 PM 32 | 33 | // You can use one of several named masks 34 | dateFormat(now, "isoDateTime"); 35 | // 2007-06-09T17:46:21 36 | 37 | // ...Or add your own 38 | dateFormat.masks.hammerTime = 'HH:MM! "Can\'t touch this!"'; 39 | dateFormat(now, "hammerTime"); 40 | // 17:46! Can't touch this! 41 | 42 | // You can also provide the date as a string 43 | dateFormat("Jun 9 2007", "fullDate"); 44 | // Saturday, June 9, 2007 45 | 46 | // Note that if you don't include the mask argument, 47 | // dateFormat.masks.default is used 48 | dateFormat(now); 49 | // Sat Jun 09 2007 17:46:21 50 | 51 | // And if you don't include the date argument, 52 | // the current date and time is used 53 | dateFormat(); 54 | // Sat Jun 09 2007 17:46:22 55 | 56 | // You can also skip the date argument (as long as your mask doesn't 57 | // contain any numbers), in which case the current date/time is used 58 | dateFormat("longTime"); 59 | // 5:46:22 PM EST 60 | 61 | // And finally, you can convert local time to UTC time. Simply pass in 62 | // true as an additional argument (no argument skipping allowed in this case): 63 | dateFormat(now, "longTime", true); 64 | // 10:46:21 PM UTC 65 | 66 | // ...Or add the prefix "UTC:" or "GMT:" to your mask. 67 | dateFormat(now, "UTC:h:MM:ss TT Z"); 68 | // 10:46:21 PM UTC 69 | 70 | // You can also get the ISO 8601 week of the year: 71 | dateFormat(now, "W"); 72 | // 42 73 | 74 | // and also get the ISO 8601 numeric representation of the day of the week: 75 | dateFormat(now, "N"); 76 | // 6 77 | ``` 78 | 79 | ### Mask options 80 | 81 | | Mask | Description | 82 | | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | 83 | | `d` | Day of the month as digits; no leading zero for single-digit days. | 84 | | `dd` | Day of the month as digits; leading zero for single-digit days. | 85 | | `ddd` | Day of the week as a three-letter abbreviation. | 86 | | `DDD` | "Ysd", "Tdy" or "Tmw" if date lies within these three days. Else fall back to ddd. | 87 | | `dddd` | Day of the week as its full name. | 88 | | `DDDD` | "Yesterday", "Today" or "Tomorrow" if date lies within these three days. Else fall back to dddd. | 89 | | `m` | Month as digits; no leading zero for single-digit months. | 90 | | `mm` | Month as digits; leading zero for single-digit months. | 91 | | `mmm` | Month as a three-letter abbreviation. | 92 | | `mmmm` | Month as its full name. | 93 | | `yy` | Year as last two digits; leading zero for years less than 10. | 94 | | `yyyy` | Year represented by four digits. | 95 | | `h` | Hours; no leading zero for single-digit hours (12-hour clock). | 96 | | `hh` | Hours; leading zero for single-digit hours (12-hour clock). | 97 | | `H` | Hours; no leading zero for single-digit hours (24-hour clock). | 98 | | `HH` | Hours; leading zero for single-digit hours (24-hour clock). | 99 | | `M` | Minutes; no leading zero for single-digit minutes. | 100 | | `MM` | Minutes; leading zero for single-digit minutes. | 101 | | `N` | ISO 8601 numeric representation of the day of the week. | 102 | | `o` | GMT/UTC timezone offset, e.g. -0500 or +0230. | 103 | | `p` | GMT/UTC timezone offset, e.g. -05:00 or +02:30. | 104 | | `s` | Seconds; no leading zero for single-digit seconds. | 105 | | `ss` | Seconds; leading zero for single-digit seconds. | 106 | | `S` | The date's ordinal suffix (st, nd, rd, or th). Works well with `d`. | 107 | | `l` | Milliseconds; gives 3 digits. | 108 | | `L` | Milliseconds; gives 2 digits. | 109 | | `t` | Lowercase, single-character time marker string: a or p. | 110 | | `tt` | Lowercase, two-character time marker string: am or pm. | 111 | | `T` | Uppercase, single-character time marker string: A or P. | 112 | | `TT` | Uppercase, two-character time marker string: AM or PM. | 113 | | `W` | ISO 8601 week number of the year, e.g. 4, 42 | 114 | | `WW` | ISO 8601 week number of the year, leading zero for single-digit, e.g. 04, 42 | 115 | | `Z` | US timezone abbreviation, e.g. EST or MDT. For non-US timezones, the GMT/UTC offset is returned, e.g. GMT-0500 | 116 | | `'...'`, `"..."` | Literal character sequence. Surrounding quotes are removed. | 117 | | `UTC:` | Must be the first four characters of the mask. Converts the date from local time to UTC/GMT/Zulu time before applying the mask. The "UTC:" prefix is removed. | 118 | 119 | ### Named Formats 120 | 121 | | Name | Mask | Example | 122 | | ----------------- | ------------------------------ | ------------------------ | 123 | | `default` | `ddd mmm dd yyyy HH:MM:ss` | Sat Jun 09 2007 17:46:21 | 124 | | `shortDate` | `m/d/yy` | 6/9/07 | 125 | | `paddedShortDate` | `mm/dd/yyyy` | 06/09/2007 | 126 | | `mediumDate` | `mmm d, yyyy` | Jun 9, 2007 | 127 | | `longDate` | `mmmm d, yyyy` | June 9, 2007 | 128 | | `fullDate` | `dddd, mmmm d, yyyy` | Saturday, June 9, 2007 | 129 | | `shortTime` | `h:MM TT` | 5:46 PM | 130 | | `mediumTime` | `h:MM:ss TT` | 5:46:21 PM | 131 | | `longTime` | `h:MM:ss TT Z` | 5:46:21 PM EST | 132 | | `isoDate` | `yyyy-mm-dd` | 2007-06-09 | 133 | | `isoTime` | `HH:MM:ss` | 17:46:21 | 134 | | `isoDateTime` | `yyyy-mm-dd'T'HH:MM:sso` | 2007-06-09T17:46:21+0700 | 135 | | `isoUtcDateTime` | `UTC:yyyy-mm-dd'T'HH:MM:ss'Z'` | 2007-06-09T22:46:21Z | 136 | 137 | ### Localization 138 | 139 | Day names, month names and the AM/PM indicators can be localized by 140 | passing an object with the necessary strings. For example: 141 | 142 | ```js 143 | var dateFormat = require("dateformat"); 144 | dateFormat.i18n = { 145 | dayNames: [ 146 | "Sun", 147 | "Mon", 148 | "Tue", 149 | "Wed", 150 | "Thu", 151 | "Fri", 152 | "Sat", 153 | "Sunday", 154 | "Monday", 155 | "Tuesday", 156 | "Wednesday", 157 | "Thursday", 158 | "Friday", 159 | "Saturday", 160 | ], 161 | monthNames: [ 162 | "Jan", 163 | "Feb", 164 | "Mar", 165 | "Apr", 166 | "May", 167 | "Jun", 168 | "Jul", 169 | "Aug", 170 | "Sep", 171 | "Oct", 172 | "Nov", 173 | "Dec", 174 | "January", 175 | "February", 176 | "March", 177 | "April", 178 | "May", 179 | "June", 180 | "July", 181 | "August", 182 | "September", 183 | "October", 184 | "November", 185 | "December", 186 | ], 187 | timeNames: ["a", "p", "am", "pm", "A", "P", "AM", "PM"], 188 | }; 189 | ``` 190 | 191 | > Notice that only one language is supported at a time and all strings 192 | > _must_ be present in the new value. 193 | 194 | ### Breaking change in 2.1.0 195 | 196 | - 2.1.0 was published with a breaking change, for those using localized strings. 197 | - 2.2.0 has been published without the change, to keep packages refering to ^2.0.0 to continue working. This is now branch v2_2. 198 | - 3.0.\* contains the localized AM/PM change. 199 | 200 | ## License 201 | 202 | (c) 2007-2009 Steven Levithan [stevenlevithan.com][stevenlevithan], MIT license. 203 | 204 | [dateformat]: http://blog.stevenlevithan.com/archives/date-time-format 205 | [stevenlevithan]: http://stevenlevithan.com/ 206 | --------------------------------------------------------------------------------