Skip to content

Commit

Permalink
Merge pull request #156 from smithalexk/master
Browse files Browse the repository at this point in the history
Adding padding to week (W)
  • Loading branch information
chase-manning authored Jan 23, 2021
2 parents 7dcd290 + 2134179 commit 71ec7d8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,8 @@ dateFormat(now, "N");
| `tt` | Lowercase, two-character time marker string: am or pm. |
| `T` | Uppercase, single-character time marker string: A or P. |
| `TT` | Uppercase, two-character time marker string: AM or PM. |
| `W` | ISO 8601 week number of the year, e.g. 42 |
| `W` | ISO 8601 week number of the year, e.g. 4, 42 |
| `WW` | ISO 8601 week number of the year, leading zero for single-digit, e.g. 04, 42 |
| `Z` | US timezone abbreviation, e.g. EST or MDT. For non-US timezones, the GMT/UTC offset is returned, e.g. GMT-0500 |
| `'...'`, `"..."` | Literal character sequence. Surrounding quotes are removed. |
| `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. |
Expand Down
3 changes: 2 additions & 1 deletion src/dateformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

(function (global) {
const dateFormat = (() => {
const token = /d{1,4}|D{3,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LlopSZWN]|"[^"]*"|'[^']*'/g;
const token = /d{1,4}|D{3,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|W{1,2}|[LlopSZN]|"[^"]*"|'[^']*'/g;
const timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g;
const timezoneClip = /[^-+\dA-Z]/g;

Expand Down Expand Up @@ -141,6 +141,7 @@
d() % 10 > 3 ? 0 : (((d() % 100) - (d() % 10) != 10) * d()) % 10
],
W: () => W(),
WW: () => pad( W() ),
N: () => N(),
};

Expand Down
30 changes: 30 additions & 0 deletions test/test_mask-ww_.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var assert = require("assert");
var dateFormat = require("../lib/dateformat");

describe("Mask: 'WW'", function () {
it("should format '1876-01-12' as '02'", function (done) {
var date = new Date("1876-01-12");
var d = dateFormat(date, "WW");
assert.strictEqual(d, "02");
done();
});

it("should format '2013-12-11' as '50'", function (done) {
var date = new Date("2013-12-11");
var d = dateFormat(date, "WW");
assert.strictEqual(d, "50");
done();
});

it("should format '2020-03-04' as '10'", function (done) {
var d = dateFormat("2020-03-04", "WW");
assert.strictEqual(d, "10");
done();
});

it("should format '2020-02-01' as '05'", function (done) {
var d = dateFormat("2020-02-01", "WW");
assert.strictEqual(d, "05");
done();
});
});

0 comments on commit 71ec7d8

Please sign in to comment.