@barchart/common-js
Version:
Library of common JavaScript utilities
75 lines (61 loc) • 1.23 kB
JavaScript
module.exports = (() => {
'use strict';
const utilities = {
getTimestamp() {
return (new Date()).getTime();
},
getShortDay(date) {
const day = date.getDay();
return days[day].short;
},
getDate(date) {
return date.getDate();
},
getDateOrdinal(date) {
const d = utilities.getDate(date);
const remainder = d % 10;
let returnRef;
if (remainder === 1 && d !== 11) {
returnRef = 'st';
} else if (remainder === 2 && d !== 12) {
returnRef = 'nd';
} else if (remainder === 3 && d !== 13) {
returnRef = 'rd';
} else {
returnRef = 'th';
}
return returnRef;
},
getShortMonth(date) {
const month = date.getMonth();
return months[month].short;
},
getYear(date) {
return date.getFullYear();
}
};
const days = [
{ short: 'Sun' },
{ short: 'Mon' },
{ short: 'Tue' },
{ short: 'Wed' },
{ short: 'Thu' },
{ short: 'Fri' },
{ short: 'Sat' }
];
const months = [
{ short: 'Jan' },
{ short: 'Feb' },
{ short: 'Mar' },
{ short: 'Apr' },
{ short: 'May' },
{ short: 'Jun' },
{ short: 'Jul' },
{ short: 'Aug' },
{ short: 'Sep' },
{ short: 'Oct' },
{ short: 'Nov' },
{ short: 'Dec' }
];
return utilities;
})();