datetimes
Version:
Extend class of Date
36 lines (33 loc) • 1.48 kB
JavaScript
/**
* 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;
}