UNPKG

datetimes

Version:

Extend class of Date

347 lines (301 loc) 9.93 kB
var ptBR = { "months": [ "janeiro", "fevereiro", "março", "abril", "maio", "junho", "julho", "agosto", "setembro", "outubro", "novembro", "dezenbro", ], "days": [ "domingo", "segunda-feira", "terça-feira", "quarta-feira", "quinta-feira", "sexta-feira", "sábado", ] } var enUS = { "months": [ "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december", ], "days": [ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", ] } var esMX = { "months": [ "enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "setiembre", "octubre", "noviembre", "diciembre", ], "days": [ "domingo", "lunes", "martes", "miércoles", "jueves", "virnes", "sábado", ] } var languages = { "pt-BR": ptBR, "en-US": enUS, "es-MX": esMX, } function DayExpression(dateRx, length, date, language) { Expresion.call(this, dateRx, length, date, language); this.oneDigit = oneDigit; this.twoDigit = twoDigit; this.threeDigit = threeDigit; this.fourDigit = fourDigit; this.otherDigit = null; function oneDigit(input) { return input.replace(this.dateRx, this.date.getDate().toString()); } function twoDigit(input) { return input.replace(this.dateRx, this.date.getDate().toString().padStart(2, '0')); } function threeDigit(input) { return input.replace(this.dateRx, this.language.days[this.date.getDay()].substr(0,3)); } function fourDigit(input) { return input.replace(this.dateRx, this.language.days[this.date.getDay()]); } } function DayPeriodExpression(dateRx, date, language) { Expresion.call(this, dateRx, date, language); this.oneDigit = null; this.twoDigit = twoDigit; this.threeDigit = null; this.fourDigit = null; this.otherDigit = null; function twoDigit(input) { var period = this.date.toLocaleString('en-US', { hour: 'numeric', hour12: true}); return input.replace(this.dateRx, period.replace(/[0-9\s]/g, '')); } } function Expresion(dateRx, date, language) { this.dateRx = dateRx; this.date = date; this.language = languages[language]; //methods this.interpret = interpret; this.oneDigit = null; this.twoDigit = null; this.threeDigit = null; this.fourDigit = null; this.otherDigit = null; //implementation function interpret(context) { if (!context.input.length) { return; } var sourceLength = this.dateRx.source.replace('\\b', '').length; switch (sourceLength) { case 1: if (this.oneDigit) context.input = this.oneDigit(context.input); break; case 2: if (this.twoDigit) context.input = this.twoDigit(context.input); break; case 3: if (this.threeDigit) context.input = this.threeDigit(context.input); break; case 4: if (this.fourDigit) context.input = this.fourDigit(context.input); break; default: if (this.otherDigit) context.input = this.otherDigit(context.input); break; } context.output = context.input; } } function HourExpression(dateRx, date, language) { Expresion.call(this, dateRx, date, language); this.oneDigit = oneDigit; this.twoDigit = twoDigit; this.threeDigit = null; this.fourDigit = null; this.otherDigit = null; function oneDigit(input) { var hour = this.date.toLocaleString('en-US', { hour: 'numeric', hour12: true}); hour = hour.replace(/ PM/g, '').replace(/ AM/g, ''); return input.replace(this.dateRx, hour.padStart(2, '0')); } function twoDigit(input) { var hour = this.date.toLocaleString('en-US', { hour: 'numeric', hour12: false}); return input.replace(this.dateRx, hour.padStart(2, '0')); } } function MillisecondExpression(dateRx, date, language) { Expresion.call(this, dateRx, date, language); this.oneDigit = null; this.twoDigit = null; this.threeDigit = threeDigit; this.fourDigit = null; this.otherDigit = null; function threeDigit(input) { return input.replace(this.dateRx, this.date.getMilliseconds().toString().padStart(3, '0')); } } function MinuteExpression(dateRx, date, language) { Expresion.call(this, dateRx, date, language); this.oneDigit = null; this.twoDigit = twoDigit; this.threeDigit = null; this.fourDigit = null; this.otherDigit = null; function twoDigit(input) { return input.replace(this.dateRx, this.date.getMinutes().toString().padStart(2, '0')); } } function MonthExpression(dateRx, date, language) { Expresion.call(this, dateRx, date, language); this.oneDigit = oneDigit; this.twoDigit = twoDigit; this.threeDigit = threeDigit; this.fourDigit = fourDigit; this.otherDigit = null; function oneDigit(input) { return input.replace(this.dateRx, (this.date.getMonth() + 1).toString()); } function twoDigit(input) { return input.replace(this.dateRx, (this.date.getMonth() + 1).toString().padStart(2, '0')); } function threeDigit(input) { return input.replace(this.dateRx, this.language.months[this.date.getMonth()].substr(0, 3)); } function fourDigit(input) { return input.replace(this.dateRx, this.language.months[this.date.getMonth()]); } } function SecondExpression(dateRx, date, language) { Expresion.call(this, dateRx, date, language); this.oneDigit = null; this.twoDigit = twoDigit; this.threeDigit = null; this.fourDigit = null; this.otherDigit = null; function twoDigit(input) { return input.replace(this.dateRx, this.date.getSeconds().toString().padStart(2, '0')); } } function YearExpression(dateRx, date, language) { Expresion.call(this, dateRx, date, language); this.oneDigit = null; this.twoDigit = twoDigit; this.threeDigit = threeDigit; this.fourDigit = fourDigit; this.otherDigit = null; function twoDigit(input) { return input.replace(this.dateRx, this.date.getFullYear().toString().substr(2)); } function threeDigit(input) { return input.replace(this.dateRx, this.date.getFullYear().toString().substr(3)); } function fourDigit(input) { return input.replace(this.dateRx, this.date.getFullYear().toString()); } } /** * Format date and time * * @param {string} stringFormat sample dd/MM/yyyy * @param {string} language pt-BR, es-MX or en-US * @returns formatted datetime */ function formatDatetimes(stringFormat, language) { var context = new Context(stringFormat); var tree = []; language = language||'pt-BR'; tree.push(new MillisecondExpression(/zzz/g, this, language)); tree.push(new SecondExpression(/ss/g, this, language)); tree.push(new MinuteExpression(/mm/g, this, language)); tree.push(new HourExpression(/HH/g, this, language)); tree.push(new HourExpression(/h/g, this, language)); tree.push(new DayExpression(/dddd/g, this, language)); tree.push(new DayExpression(/ddd/g, this, language)); tree.push(new DayExpression(/dd/g, this, language)); tree.push(new DayExpression(/d\b/g, this, language)); tree.push(new MonthExpression(/MMMM/g, this, language)); tree.push(new MonthExpression(/MMM/g, this, language)); tree.push(new MonthExpression(/MM/g, this, language)); tree.push(new MonthExpression(/M\b/g, this, language)); tree.push(new YearExpression(/yyyy/g, this, language)); tree.push(new YearExpression(/yyy/g, this, language)); tree.push(new YearExpression(/yy/g, this, language)); tree.push(new DayPeriodExpression(/dp\b/g, this, language)); tree.forEach(function(expresion) { expresion.interpret(context); }); return context.output; } function noTimeZoneDatetimes(year, month, date, hours, minutes, seconds, ms) { year = year | 0; month = month | 0; date = date | 0; hours = hours | 0; minutes = minutes | 0; seconds = seconds | 0; ms = ms | 0; var data = new Date(Date.UTC(year, month, date, hours, minutes, seconds, ms)); data = new Date(data.setHours(data.getHours() + (data.getTimezoneOffset()/60))); return data; } function Context(input) { this.input = input; this.output = ''; } 'use strict'; function Datetimes() { var x = new (Function.prototype.bind.apply(Date, [Date].concat(Array.prototype.slice.call(arguments)))) x.__proto__ = Datetimes.prototype; return x; } //Methods Datetimes.prototype.__proto__ = Date.prototype; Datetimes.prototype.format = formatDatetimes; Datetimes.prototype.now = function() { return new Datetimes(); } Datetimes.prototype.noTimeZone = function() { return new Datetimes(noTimeZoneDatetimes.apply(noTimeZoneDatetimes, arguments)); }; if (module) module.exports = Datetimes;