2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* Date handling utilities.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2020-05-04 19:55:18 +00:00
|
|
|
Date.prototype.clone = function() {
|
|
|
|
return new Date(this.getTime());
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2023-10-09 19:01:11 +00:00
|
|
|
Date.vnUTC = () => {
|
|
|
|
const env = process.env.NODE_ENV;
|
|
|
|
if (!env || env === 'development')
|
|
|
|
return new Date(Date.UTC(2001, 0, 1, 11));
|
|
|
|
|
|
|
|
return new Date();
|
|
|
|
};
|
|
|
|
|
|
|
|
Date.vnNew = () => {
|
|
|
|
return new Date(Date.vnUTC());
|
|
|
|
};
|
|
|
|
|
|
|
|
Date.vnNow = () => {
|
|
|
|
return new Date(Date.vnUTC()).getTime();
|
|
|
|
};
|
|
|
|
|
2016-09-26 09:28:47 +00:00
|
|
|
module.exports =
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2022-05-30 01:30:33 +00:00
|
|
|
WDays: [
|
2016-10-15 18:58:30 +00:00
|
|
|
'Sunday'
|
|
|
|
,'Monday'
|
|
|
|
,'Tuesday'
|
|
|
|
,'Wednesday'
|
|
|
|
,'Thursday'
|
|
|
|
,'Friday'
|
|
|
|
,'Saturday'
|
2015-01-23 13:09:30 +00:00
|
|
|
]
|
2022-05-30 01:30:33 +00:00
|
|
|
,AbrWDays: [
|
2016-10-15 18:58:30 +00:00
|
|
|
'Su'
|
|
|
|
,'Mo'
|
|
|
|
,'Tu'
|
|
|
|
,'We'
|
|
|
|
,'Th'
|
|
|
|
,'Fr'
|
|
|
|
,'Sa'
|
2015-01-23 13:09:30 +00:00
|
|
|
]
|
2022-05-30 01:30:33 +00:00
|
|
|
,Months: [
|
2016-10-15 18:58:30 +00:00
|
|
|
'January'
|
|
|
|
,'February'
|
|
|
|
,'March'
|
|
|
|
,'April'
|
|
|
|
,'May'
|
|
|
|
,'June'
|
|
|
|
,'July'
|
|
|
|
,'August'
|
|
|
|
,'September'
|
|
|
|
,'October'
|
|
|
|
,'November'
|
|
|
|
,'December'
|
2015-01-23 13:09:30 +00:00
|
|
|
]
|
2022-05-30 01:30:33 +00:00
|
|
|
,AbrMonths: [
|
2016-10-15 18:58:30 +00:00
|
|
|
'Jan'
|
|
|
|
,'Feb'
|
|
|
|
,'Mar'
|
|
|
|
,'Apr'
|
2022-05-24 10:18:44 +00:00
|
|
|
,'AbrMay'
|
2016-10-15 18:58:30 +00:00
|
|
|
,'Jun'
|
|
|
|
,'Jul'
|
|
|
|
,'Ago'
|
|
|
|
,'Sep'
|
|
|
|
,'Oct'
|
|
|
|
,'Nov'
|
|
|
|
,'Dec'
|
2015-01-23 13:09:30 +00:00
|
|
|
]
|
|
|
|
|
2023-02-23 19:19:56 +00:00
|
|
|
,tokenD: '%A, %B %e %Y'
|
2016-01-15 12:31:08 +00:00
|
|
|
|
2020-05-04 19:55:18 +00:00
|
|
|
,regexp: new RegExp('%[a-zA-Z]', 'g')
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,pad(number) {
|
2015-01-23 13:09:30 +00:00
|
|
|
if (number < 10)
|
2020-05-04 19:55:18 +00:00
|
|
|
return '0'+ number.toString();
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2020-05-04 19:55:18 +00:00
|
|
|
return number.toString();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,regexpFunc(d, token) {
|
2020-05-04 19:55:18 +00:00
|
|
|
switch (token.charAt(1)) {
|
2015-01-23 13:09:30 +00:00
|
|
|
// Minutes with 2 digits
|
2020-05-04 19:55:18 +00:00
|
|
|
case 'M': return this.pad(d.getMinutes());
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
// Hour with 2 digits in 24 hour format
|
2020-05-04 19:55:18 +00:00
|
|
|
case 'H': return this.pad(d.getHours());
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
// Complete time
|
2020-05-04 19:55:18 +00:00
|
|
|
case 'T': return d.toLocaleTimeString();
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
// Complete week day name
|
2020-05-04 19:55:18 +00:00
|
|
|
case 'A': return _(this.WDays[d.getDay()]);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
// Abreviated week day name
|
2020-05-04 19:55:18 +00:00
|
|
|
case 'a': return _(this.AbrWDays[d.getDay()]);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
// Day of month with 2 digits
|
2020-05-04 19:55:18 +00:00
|
|
|
case 'd': return this.pad(d.getDate());
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
// Day of month
|
2020-05-04 19:55:18 +00:00
|
|
|
case 'e': return d.getDate();
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
// Complete month name
|
2020-05-04 19:55:18 +00:00
|
|
|
case 'B': return _(this.Months[d.getMonth()]);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
// Abreviated month name
|
2020-05-04 19:55:18 +00:00
|
|
|
case 'b': return _(this.AbrMonths[d.getMonth()]);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
// Month number with 2 digits
|
2020-05-04 19:55:18 +00:00
|
|
|
case 'm': return this.pad(d.getMonth() + 1);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
// Year with 4 digits
|
2020-05-04 19:55:18 +00:00
|
|
|
case 'Y': return d.getFullYear();
|
2016-01-15 12:31:08 +00:00
|
|
|
|
2023-02-23 19:19:56 +00:00
|
|
|
// Complete date
|
2020-05-04 19:55:18 +00:00
|
|
|
case 'D': return _(this.tokenD).replace(this.regexp, this.regexpFunc.bind(this, d));
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return token;
|
|
|
|
}
|
|
|
|
|
2022-11-16 01:46:44 +00:00
|
|
|
,strftime(date, format) {
|
2015-01-23 13:09:30 +00:00
|
|
|
if (!date)
|
|
|
|
return '';
|
2020-05-04 19:55:18 +00:00
|
|
|
if (!(date instanceof Date))
|
|
|
|
date = new Date(date);
|
2021-01-23 16:15:02 +00:00
|
|
|
if (isNaN(date.getTime()))
|
|
|
|
return date.toString();
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2020-05-04 19:55:18 +00:00
|
|
|
return format.replace(this.regexp, this.regexpFunc.bind(this, date));
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
};
|