forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 5
/
date.js
136 lines (121 loc) · 2.95 KB
/
date.js
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
const utils = require('./utils');
const moment = require('moment');
/**
* @exports date
*/
const helpers = module.exports;
/**
* Get the current year.
*
* ```handlebars
* {{year}}
* <!-- 2017 -->
* {{year "YY"}}
* <!-- 17 -->
* ```
*
* @param {String} `pattern`
* @return {String}
* @block
* @api public
*/
helpers.year = function(pattern) {
const year = new Date().getUTCFullYear().toString();
if (typeof pattern === 'string') {
if (/[Yy]{4}/.test(pattern)) {
return year;
}
if (/[Yy]{2}/.test(pattern)) {
return year.substr(2, 2);
}
}
return year;
};
/**
* Formats a date (now, or a specific date) with a format
*
* ```handlebars
* {{date}}
* <!-- December 31, 2018 -->
* {{date "YYYY"}}
* <!-- 2017 -->
* {{date "2018-12-31" "MMMM DD, YYYY"}}
* <!-- December 31, 2018 -->
* ```
* @param {String} `str` [optional] The stringified date to format
* @param {String} `pattern` [optional] The pattern to use when formatting the date
* @block
* @api public
*/
helpers.moment = function dateHelper(str, pattern) {
if (utils.isOptions(pattern)) {
pattern = null;
}
if (utils.isOptions(str)) {
pattern = null;
str = null;
}
// if no args are passed, return a formatted date
if (str == null && pattern == null) {
moment.locale('en');
return moment().format('MMMM DD, YYYY');
}
// if both args are strings, format the first (date) using the second (format)
if (typeof str === 'string' && typeof pattern === 'string') {
return moment(str).format(pattern);
}
// if only a string is passed, assume it's a date pattern ('YYYY')
if (typeof str === 'string' && !pattern) {
return moment().format(str);
}
return moment(str).format(pattern);
};
/**
* @type function
* @see {@link moment}
*/
helpers.date = (...args) => helpers.moment(...args);
/**
* Formats a specific date with a format (or ISO8601 if no format specified)
*
* ```handlebars
* {{formatDate myDate}}
* <!-- '2017-01-18T10:54:00' -->
* {{formatDate date 'dddd'}}
* <!-- 'Wednesday' -->
* ```
* @param {String} date - The date to format
* @param {String} pattern - [optional] The pattern to use when formatting the date
* @api public
*/
helpers.formatDate = function(date, _format) {
let format = _format;
if (!format || !utils.isString(format)) format = undefined;
return date ? moment(date).utc().format(format).replace('Z', '') : '';
};
/**
* Formats a specific date as a more readable date
*
* ```handlebars
* {{niceDate myDate}}
* <!-- 'Wed 18 01 2017' -->
* ```
* @param {String} date - The date to format
* @api public
*/
helpers.niceDate = function(date) {
return date ? moment(date).format('ddd DD MM YYYY') : '';
};
/**
* Formats a specific date as just the time component
*
* ```handlebars
* {{getTime myDate}}
* <!-- '10:54' -->
* ```
* @param {String} date - The date to format
* @api public
*/
helpers.getTime = function(date) {
return date ? moment(date).format('HH:mm') : '';
};