forked from csvenja/javascript-exercises
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpretty-date.js
More file actions
26 lines (24 loc) · 893 Bytes
/
Copy pathpretty-date.js
File metadata and controls
26 lines (24 loc) · 893 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* ======================================================
https://github.com/phstc/jquery-dateFormat
yy = short year(17)
yyyy = long year(2017)
MM = month (01-12)
MMM = month abbreviation (Jan, Feb ... Dec)
dd = day (01 - 31)
ddd = day of the week in words (Monday, Tuesday ... Sunday)
*/
function getPrettyDate(tpl, date) {
const d = date ? new Date(date) : new Date();
const slice = String.prototype.slice;
const strg = {
'yy': String.prototype.substr.call(d.getFullYear(), 2),
'yyyy': d.getFullYear(),
'MM': slice.call('0' + (d.getMonth() + 1), -2),
'MMM': ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'][d.getMonth() + 1],
'dd': slice.call('0' + d.getDate(), -2),
'ddd': ['Sun', 'Mon', 'Tue', 'Wen', 'Thu', 'Fry', 'Sut'][d.getDay()],
};
return tpl.replace(/\w+/g, (v) => strg[v]);
}
getPrettyDate('dd/MM/yyyy');
getPrettyDate('ddd/MMM/yyyy');