Coverage

100%
38
38
0

/Users/sebastiansandqvist/Documents/Sites & Projects/apps/#modules/s-date/index.js

100%
38
38
0
LineHitsSource
11'use strict';
2
3// ----- Date formatter
4// -- @param format {String}
5// -- @param date {Date} optional date context
6// -- @return {String}
7// ---------------------------------------
81module.exports = function formatDate(format, date) {
9
10 // default to today
1178 if (!date) {
123 date = new Date();
13 }
14
15 // ----- years
16 // ---------------------------------------
1778 var yyyy = date.getFullYear().toString(); // {yyyy}
1878 var yy = yyyy.slice(-2); // {yy}
19
20 // ----- months
21 // ---------------------------------------
2278 var monthInt = date.getMonth() + 1;
2378 var m = monthInt.toString(); // {m}
2478 var mm = monthInt < 10 ? '0' + m : m; // {mm}
2578 var months = [
26 'January',
27 'February',
28 'March',
29 'April',
30 'May',
31 'June',
32 'July',
33 'August',
34 'September',
35 'October',
36 'November',
37 'December'
38 ];
39
4078 var month = months[date.getMonth()]; // {Month} & {Mo}
41
42
43 // ----- days
44 // ---------------------------------------
4578 var day = date.getDate();
4678 var dd = day < 10 ? '0' + day : day; // {dd}
47
48
49 //----------------- suffixes -----------------
5078 var daySuffixes = Object.create(null); // so for in loop works
5178 daySuffixes.st = [1, 21, 31];
5278 daySuffixes.nd = [2, 22];
5378 daySuffixes.rd = [3, 23];
54
55 // loop through 'st', 'nd', and 'rd' (suffixes like '1st', '2nd', etc)
56 // if suffix is not found, it is 'th' (most numbers)
5778 var suffixNotFound;
58
5978 for (var suffix in daySuffixes) {
60177 if (daySuffixes[suffix].indexOf(day) > -1) {
6156 suffixNotFound = false;
6256 break;
63 }
64121 suffixNotFound = true;
65 }
66
6778 if (suffixNotFound) {
6822 suffix = 'th';
69 }
70
7178 var dayWithSuffix = day + suffix; // {D}
72 //----------------- end suffixes -----------------
73
7478 var weekdays = [
75 'Monday',
76 'Tuesday',
77 'Wednesday',
78 'Thursday',
79 'Friday',
80 'Saturday',
81 'Sunday'
82 ];
83
8478 var weekday = weekdays[date.getDay()];
85
86
87 // ----- hours
88 // ---------------------------------------
8978 var hours24 = date.getHours();
9078 var hh24 = hours24 < 10 ? '0' + hours24 : hours24;
9178 var hours = hours24 % 12 === 0 ? 12 : hours24 % 12; // {h}
9278 var hh = hours < 10 ? '0' + hours : hours;
9378 var ampm = hours24 < 12 ? 'am' : 'pm'; // {ampm}
94
95 // ----- minutes, seconds
96 // ---------------------------------------
9778 var min = date.getMinutes();
9878 var minutes = min < 10 ? '0' + min : min;
99
10078 var sec = date.getSeconds();
10178 var seconds = sec < 10 ? '0' + sec : sec;
102
10378 return format
104 .replace('{yyyy}', yyyy)
105 .replace('{yy}', yy)
106 .replace('{m}', m)
107 .replace('{mm}', mm)
108 .replace('{Month}', month)
109 .replace('{Mo}', month.substr(0,3))
110 .replace('{d}', day)
111 .replace('{dd}', dd)
112 .replace('{ds}', dayWithSuffix)
113 .replace('{Weekday}', weekday)
114 .replace('{Day}', weekday.substr(0,3))
115 .replace('{Dy}', weekday.substr(0,2))
116 .replace('{D}', weekday[0])
117 .replace('{h24}', hours24)
118 .replace('{hh24}', hh24)
119 .replace('{h}', hours)
120 .replace('{hh}', hh)
121 .replace('{ampm}', ampm)
122 .replace('{AMPM}', ampm.toUpperCase())
123 .replace('{Minutes}', minutes)
124 .replace('{Seconds}', seconds);
125
126};