0
1
Fork 0
hedera-web-mindshore/js/vn/date.js

122 lines
2.0 KiB
JavaScript
Raw Normal View History

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