hedera-web/js/vn/date.js

138 lines
2.3 KiB
JavaScript

/**
* Date handling utilities.
*/
Date.prototype.clone = function() {
return new Date(this.getTime());
}
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();
};
module.exports =
{
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'
,'AbrMay'
,'Jun'
,'Jul'
,'Ago'
,'Sep'
,'Oct'
,'Nov'
,'Dec'
]
,tokenD: '%A, %B %e %Y'
,regexp: new RegExp('%[a-zA-Z]', 'g')
,pad(number) {
if (number < 10)
return '0'+ number.toString();
return number.toString();
}
,regexpFunc(d, token) {
switch (token.charAt(1)) {
// Minutes with 2 digits
case 'M': return this.pad(d.getMinutes());
// Hour with 2 digits in 24 hour format
case 'H': return this.pad(d.getHours());
// Complete time
case 'T': return d.toLocaleTimeString();
// Complete week day name
case 'A': return _(this.WDays[d.getDay()]);
// Abreviated week day name
case 'a': return _(this.AbrWDays[d.getDay()]);
// Day of month with 2 digits
case 'd': return this.pad(d.getDate());
// Day of month
case 'e': return d.getDate();
// Complete month name
case 'B': return _(this.Months[d.getMonth()]);
// Abreviated month name
case 'b': return _(this.AbrMonths[d.getMonth()]);
// Month number with 2 digits
case 'm': return this.pad(d.getMonth() + 1);
// Year with 4 digits
case 'Y': return d.getFullYear();
// Complete date
case 'D': return _(this.tokenD).replace(this.regexp, this.regexpFunc.bind(this, d));
}
return token;
}
,strftime(date, format) {
if (!date)
return '';
if (!(date instanceof Date))
date = new Date(date);
if (isNaN(date.getTime()))
return date.toString();
return format.replace(this.regexp, this.regexpFunc.bind(this, date));
}
};