Skip to content

Commit fa071e5

Browse files
Merge pull request #71 from TalhaAwan/master
Provide masks DDD and DDDD to print "Yesterday", "Today" or "Tomorrow".
2 parents 1a5763c + 4f01cc4 commit fa071e5

3 files changed

Lines changed: 143 additions & 3 deletions

File tree

Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ dateFormat(now, "N");
8282
| `d` | Day of the month as digits; no leading zero for single-digit days. |
8383
| `dd` | Day of the month as digits; leading zero for single-digit days. |
8484
| `ddd` | Day of the week as a three-letter abbreviation. |
85+
| `DDD` | "Yes", "Tod" or "Tom" if date lies within these three days. Else fall back to ddd. |
8586
| `dddd` | Day of the week as its full name. |
87+
| `DDDD` | "Yesterday", "Today" or "Tomorrow" if date lies within these three days. Else fall back to dddd. |
8688
| `m` | Month as digits; no leading zero for single-digit months. |
8789
| `mm` | Month as digits; leading zero for single-digit months. |
8890
| `mmm` | Month as a three-letter abbreviation. |

src/dateformat.js

Lines changed: 53 additions & 3 deletions
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}|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?|[LlopSZWN]|"[^"]*"|'[^']*'/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

@@ -66,11 +66,27 @@
6666
const o = () => (utc ? 0 : date.getTimezoneOffset());
6767
const W = () => getWeek(date);
6868
const N = () => getDayOfWeek(date);
69+
6970
const flags = {
7071
d: () => d(),
7172
dd: () => pad(d()),
7273
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+
}),
7382
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+
}),
7490
m: () => m() + 1,
7591
mm: () => pad(m() + 1),
7692
mmm: () => dateFormat.i18n.monthNames[m()],
@@ -107,8 +123,8 @@
107123
gmt
108124
? "GMT"
109125
: utc
110-
? "UTC"
111-
: (String(date).match(timezone) || [""])
126+
? "UTC"
127+
: (String(date).match(timezone) || [""])
112128
.pop()
113129
.replace(timezoneClip, "")
114130
.replace(/GMT\+0000/g, "UTC"),
@@ -210,6 +226,40 @@
210226
return val;
211227
};
212228

229+
/**
230+
* Get day name
231+
* Yesterday, Today, Tomorrow if the date lies within, else fallback to Monday - Sunday
232+
* @param {Object}
233+
* @return {String}
234+
*/
235+
const getDayName = ({ y, m, D, _, dayName, short = false }) => {
236+
const today = new Date();
237+
const yesterday = new Date();
238+
yesterday.setDate(yesterday[_ + 'Date']() - 1);
239+
const tomorrow = new Date();
240+
tomorrow.setDate(tomorrow[_ + 'Date']() + 1);
241+
const today_D = () => today[_ + 'Day']();
242+
const today_m = () => today[_ + 'Month']();
243+
const today_y = () => today[_ + 'FullYear']();
244+
const yesterday_D = () => yesterday[_ + 'Day']();
245+
const yesterday_m = () => yesterday[_ + 'Month']();
246+
const yesterday_y = () => yesterday[_ + 'FullYear']();
247+
const tomorrow_D = () => tomorrow[_ + 'Day']();
248+
const tomorrow_m = () => tomorrow[_ + 'Month']();
249+
const tomorrow_y = () => tomorrow[_ + 'FullYear']();
250+
251+
if (today_y() === y && today_m() === m && today_D() === D) {
252+
return short ? 'Tod' : 'Today';
253+
}
254+
else if (yesterday_y() === y && yesterday_m() === m && yesterday_D() === D) {
255+
return short ? 'Yes' : 'Yesterday';
256+
}
257+
else if (tomorrow_y() === y && tomorrow_m() === m && tomorrow_D() === D) {
258+
return short ? 'Tom' : 'Tomorrow';
259+
}
260+
return dayName;
261+
};
262+
213263
/**
214264
* Get the ISO 8601 week number
215265
* Based on comments from

test/test_threedays.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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', 'Yes', 'Tod', 'Tom'];
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 "Yes" (Today - 1 day)', function (done) {
21+
date.setDate(date.getDate() - 1);
22+
DDD = dateFormat(date, 'DDD');
23+
assert.strictEqual(DDD, "Yes");
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 "Tod" (Today)', function (done) {
32+
DDD = dateFormat(date, 'DDD');
33+
assert.strictEqual(DDD, "Tod");
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 "Tom" (Today + 1 day)', function (done) {
43+
date.setDate(date.getDate() + 1);
44+
DDD = dateFormat(date, 'DDD');
45+
assert.strictEqual(DDD, "Tom");
46+
done();
47+
});
48+
it('should not return "Yesterday", "Today", "Tomorrow", "Yes", "Tod", or "Tom" (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", "Yes", "Tod", or "Tom" (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+
});

0 commit comments

Comments
 (0)