From 708311d693402bf0bf18131399f7e0b4d2595385 Mon Sep 17 00:00:00 2001 From: Eissa Soubhi Date: Mon, 11 Apr 2016 23:05:20 +0200 Subject: [PATCH] Update dateFormat.js added local option to display days and moths in different languages. other languages and options could be added, --- src/dateFormat.js | 60 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/src/dateFormat.js b/src/dateFormat.js index bcce734..c97313e 100644 --- a/src/dateFormat.js +++ b/src/dateFormat.js @@ -1,48 +1,60 @@ var DateFormat = {}; (function($) { - var daysInWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; - var shortDaysInWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; - var shortMonthsInYear = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', - 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; - var longMonthsInYear = ['January', 'February', 'March', 'April', 'May', 'June', - 'July', 'August', 'September', 'October', 'November', 'December']; - var shortMonthsToNumber = { 'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', - 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12' }; + var options = {local: 'en'}; + + var daysInWeek = {'en' : ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], + 'fr' : ['Dimanche', 'Lundi', 'Mardi', 'Mercredi', 'Jeudi', 'Vendredi', 'Samedi']}; + + var shortDaysInWeek = {'en' : ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], + 'fr' : ['Dim', 'Lun', 'Mar', 'Mer', 'Jeu', 'Ven', 'Sam']}; + + var shortMonthsInYear = {'en' : ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], + 'fr' : ['Janv.', 'Févr.', 'Mars', 'Avr.', 'Mai', 'Juin', 'Juill.', 'Août', 'Sept.', 'Oct.', 'Nov.', 'Déc.']}; + + var longMonthsInYear = {'en' : ['January', 'February', 'March', 'April', 'May', 'June', + 'July', 'August', 'September', 'October', 'November', 'December'], + 'fr' : ['Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', + 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre']}; + + var shortMonthsToNumber = {'en' : { 'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', + 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12' }, + 'fr' : { 'Janv.': '01', 'Févr.': '02', 'Mars': '03', 'Avr.': '04', 'Mai': '05', 'Juin': '06', + 'Juill.': '07', 'Août': '08', 'Sept.': '09', 'Oct.': '10', 'Nov.': '11', 'Déc.': '12' }}; var YYYYMMDD_MATCHER = /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.?\d{0,3}[Z\-+]?(\d{2}:?\d{2})?/; - $.format = (function() { + $.format = (function() { function numberToLongDay(value) { // 0 to Sunday // 1 to Monday - return daysInWeek[parseInt(value, 10)] || value; + return daysInWeek[options.local][parseInt(value, 10)] || value; } function numberToShortDay(value) { // 0 to Sun // 1 to Mon - return shortDaysInWeek[parseInt(value, 10)] || value; + return shortDaysInWeek[options.local][parseInt(value, 10)] || value; } function numberToShortMonth(value) { // 1 to Jan // 2 to Feb var monthArrayIndex = parseInt(value, 10) - 1; - return shortMonthsInYear[monthArrayIndex] || value; + return shortMonthsInYear[options.local][monthArrayIndex] || value; } function numberToLongMonth(value) { // 1 to January // 2 to February var monthArrayIndex = parseInt(value, 10) - 1; - return longMonthsInYear[monthArrayIndex] || value; + return longMonthsInYear[options.local][monthArrayIndex] || value; } function shortMonthToNumber(value) { // Jan to 01 // Feb to 02 - return shortMonthsToNumber[value] || value; + return shortMonthsToNumber[options.local][value] || value; } function parseTime(value) { @@ -194,9 +206,27 @@ var DateFormat = {}; return parsedDate; }, + + extend : function(out) { + // source http://youmightnotneedjquery.com + out = out || {}; + + for (var i = 1; i < arguments.length; i++) { + if (!arguments[i]) + continue; + + for (var key in arguments[i]) { + if (arguments[i].hasOwnProperty(key)) + out[key] = arguments[i][key]; + } + } - date : function(value, format) { + return out; + }, + + date : function(value, format, opts) { try { + options = this.extend( {}, options, opts ); var parsedDate = this.parseDate(value); if(parsedDate === null) {