napicuformatter
Version:
Script for formatting data
273 lines • 6.83 kB
JavaScript
import { NapicuDefaultConfig } from './config';
var NapicuDateConfig = {
days: NapicuDefaultConfig.days,
months: NapicuDefaultConfig.months,
shortNameLength: NapicuDefaultConfig.shortNameLength,
};
export class NapicuDate {
constructor(yearOrTimestamp, month, day, hours, minutes, seconds, ms) {
let date = new Date();
if (yearOrTimestamp) {
if (month)
date = new Date(yearOrTimestamp, month - 1 || 0, day || 0, hours || 0, minutes || 0, seconds || 0, ms || 0);
else
date = new Date(yearOrTimestamp);
}
this._date = date;
this._formats = {
'%yyyy': this.getYear,
'%ddn': this.getDayName,
'%MMN': this.getMonthName,
'%dn': this.getShortDayName,
'%MN': this.getShortMonthName,
'%MM': this.getMonth,
'%DMAX': this.getMaxDaysInMonth,
'%dd': this.getDay,
'%dt': this.getDate,
'%HH': this.getHours24,
'%hh': this.getHours12,
'%mm': this.getMinutes,
'%ss': this.getSeconds,
'%a': this.getMeridian,
'%z': this.getTimezone,
};
}
/**
* Format the date
* * %yyyy - Year
* * %MM - Month
* * %MMN - Month name
* * %MN - Month name short
* * %dd - Day
* * %dt - Date
* * %ddn - Day name
* * %dn - Day name short
* * %DMAX - Max days in current month
* * %HH - 24 Hour
* * %hh - 12 Hour
* * %mm - Minutes
* * %ss - Seconds
* * %a - AM/PM
* * %z - Timezone
* @param format
*/
format(format) {
let date = this._date;
let out = format;
for (const key of Object.keys(this._formats)) {
let regex = new RegExp(key, 'g');
out = out.replace(regex, this._formats[key](date));
}
return out;
}
/**
* Sets the config language
* @param config
*/
static use(config) {
NapicuDateConfig.days = config.days || NapicuDefaultConfig.days;
NapicuDateConfig.months = config.months || NapicuDefaultConfig.months;
NapicuDateConfig.shortNameLength = config.shortNameLength || NapicuDefaultConfig.shortNameLength;
}
/**
* Gets the day
* @param date
*/
getDay(date) {
return date.getDay().toString();
}
/**
* Gets the date
* @param date
*/
getDate(date) {
return date.getDate().toString();
}
/**
* Gets the month
* @param date
*/
getMonth(date) {
return (date.getMonth() + 1).toString();
}
/**
* Gets the month name
* @param date
*/
getMonthName(date) {
return NapicuDateConfig.months[date.getMonth()];
}
/**
* Gets the month name short
*/
getShortMonthName(date) {
return NapicuDateConfig.months[date.getMonth()].slice(0, NapicuDateConfig.shortNameLength);
}
/**
* Gets the day name
* @param date
*/
getDayName(date) {
return NapicuDateConfig.days[date.getDay() - 1];
}
/**
* Gets the day name short
* @param date
*/
getShortDayName(date) {
return NapicuDateConfig.days[date.getDay()].slice(0, NapicuDateConfig.shortNameLength);
}
/**
* Gets the year
* @param date
*/
getYear(date) {
return date.getFullYear().toString();
}
/**
* Gets maximum days in month
*/
getMaxDaysInMonth(date) {
return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
}
/**
* Gets the hours in 24-hour format
* @param date
*/
getHours24(date) {
return date.getHours().toString();
}
/**
* Gets the hours in 12-hour format
* @param date
*/
getHours12(date) {
let hours = date.getHours();
return hours > 12 ? (hours - 12).toString() : hours.toString();
}
/**
* Gets the minutes
* @param date
*/
getMinutes(date) {
return date.getMinutes().toString().padStart(2, '0');
}
/**
* Gets the seconds
* @param date
*/
getSeconds(date) {
return date.getSeconds().toString().padStart(2, '0');
}
/**
* Gets the meridian
* @param date
*/
getMeridian(date) {
return date.getHours() >= 12 ? 'PM' : 'AM';
}
/**
* Gets the timezone
* @param date
*/
getTimezone(date) {
return date.getTimezoneOffset().toString().padStart(2, '0');
}
/**
* Gets the days
*/
static getLanguageDays() {
return NapicuDateConfig.days;
}
/**
* Gets the months
*/
static getLanguageMonths() {
return NapicuDateConfig.months;
}
/**
* Gets the short days
*/
static getLanguageShortsDays() {
return NapicuDateConfig.days.map((day) => day.slice(0, NapicuDateConfig.shortNameLength));
}
/**
* Gets the short months
*/
static getLanguageShortsMonths() {
return NapicuDateConfig.months.map((month) => month.slice(0, NapicuDateConfig.shortNameLength));
}
/**
* Gets the day of the week, using local time
*/
getCurrentDay() {
return this._date.getDay();
}
/**
* Gets the day-of-the-month, using local time
*/
getCurrentDate() {
return this._date.getDate();
}
/**
* Gets the current month
*/
getCurrentMonth() {
return this._date.getMonth() + 1;
}
/**
* Gets the current year
*/
getCurrentYear() {
return this._date.getFullYear();
}
/**
* Gets the current seconds
*/
getCurrentSeconds() {
return this._date.getSeconds();
}
/**
* Gets the current minutes
*/
getCurrentMinutes() {
return this._date.getMinutes();
}
/**
* Gets the current minutes
*/
getCurrentHours() {
return this._date.getHours();
}
/**
* Gets the current meridian
*/
getCurrentMeridian() {
return this.getMeridian(this._date);
}
/**
* Gets the current day name
*/
getCurrentDayName() {
return NapicuDateConfig.days[this._date.getDay() - 1];
}
/**
* Gets the current month name
*/
getCurrentMonthName() {
return NapicuDateConfig.months[this._date.getMonth()];
}
/**
* Gets the time value in milliseconds.
*/
getTimeStamp() {
return this._date.getTime();
}
/**
* Gets maximum days in current month
*/
getMaxDaysInCurrentMonth() {
return new Date(this._date.getFullYear(), this._date.getMonth() + 2, 0).getDate();
}
}
//# sourceMappingURL=index.js.map