hedera-web/js/vn/date.js

126 lines
2.0 KiB
JavaScript
Raw Normal View History

/**
* Date handling utilities.
**/
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 =
{
2020-05-04 19:55:18 +00:00
WDays:
[
'Sunday'
,'Monday'
,'Tuesday'
,'Wednesday'
,'Thursday'
,'Friday'
,'Saturday'
]
,AbrWDays:
[
'Su'
,'Mo'
,'Tu'
,'We'
,'Th'
,'Fr'
,'Sa'
]
,Months:
[
'January'
,'February'
,'March'
,'April'
,'May'
,'June'
,'July'
,'August'
,'September'
,'October'
,'November'
,'December'
]
,AbrMonths:
[
'Jan'
,'Feb'
,'Mar'
,'Apr'
,'May'
,'Jun'
,'Jul'
,'Ago'
,'Sep'
,'Oct'
,'Nov'
,'Dec'
]
,tokenD: '%A, %B %e'
2020-05-04 19:55:18 +00:00
,regexp: new RegExp('%[a-zA-Z]', 'g')
2020-05-04 19:55:18 +00:00
,pad: function(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();
}
2020-05-04 19:55:18 +00:00
,regexpFunc: function(d, token) {
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 without year
2020-05-04 19:55:18 +00:00
case 'D': return _(this.tokenD).replace(this.regexp, this.regexpFunc.bind(this, d));
}
return token;
}
2020-05-04 19:55:18 +00:00
,strftime: function(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));
}
};