Skip to content

Commit 71ec7d8

Browse files
Merge pull request #156 from smithalexk/master
Adding padding to week (W)
2 parents 7dcd290 + 2134179 commit 71ec7d8

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

Readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ dateFormat(now, "N");
109109
| `tt` | Lowercase, two-character time marker string: am or pm. |
110110
| `T` | Uppercase, single-character time marker string: A or P. |
111111
| `TT` | Uppercase, two-character time marker string: AM or PM. |
112-
| `W` | ISO 8601 week number of the year, e.g. 42 |
112+
| `W` | ISO 8601 week number of the year, e.g. 4, 42 |
113+
| `WW` | ISO 8601 week number of the year, leading zero for single-digit, e.g. 04, 42 |
113114
| `Z` | US timezone abbreviation, e.g. EST or MDT. For non-US timezones, the GMT/UTC offset is returned, e.g. GMT-0500 |
114115
| `'...'`, `"..."` | Literal character sequence. Surrounding quotes are removed. |
115116
| `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. |

src/dateformat.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

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

@@ -141,6 +141,7 @@
141141
d() % 10 > 3 ? 0 : (((d() % 100) - (d() % 10) != 10) * d()) % 10
142142
],
143143
W: () => W(),
144+
WW: () => pad( W() ),
144145
N: () => N(),
145146
};
146147

test/test_mask-ww_.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
});

0 commit comments

Comments
 (0)