[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/media/system/js/fields/calendar-locales/date/gregorian/ -> date-helper.js (source)

   1  !(function(Date){
   2      'use strict';
   3  
   4      var localNumbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
   5  
   6      /****************** Gregorian dates ********************/
   7      /** Constants used for time computations */
   8      Date.SECOND = 1000 /* milliseconds */;
   9      Date.MINUTE = 60 * Date.SECOND;
  10      Date.HOUR   = 60 * Date.MINUTE;
  11      Date.DAY    = 24 * Date.HOUR;
  12      Date.WEEK   =  7 * Date.DAY;
  13  
  14      /** MODIFY ONLY THE MARKED PARTS OF THE METHODS **/
  15      /************ START *************/
  16      /** INTERFACE METHODS FOR THE CALENDAR PICKER **/
  17  
  18      /********************** *************************/
  19      /**************** SETTERS ***********************/
  20      /********************** *************************/
  21  
  22      /** Sets the date for the current date without h/m/s. */
  23      Date.prototype.setLocalDateOnly = function (dateType, date) {
  24          if (dateType != 'gregorian') {
  25              /** Modify to match the current calendar when overriding **/
  26              return '';
  27          } else {
  28              var tmp = new Date(date);
  29              this.setDate(1);
  30              this.setFullYear(tmp.getFullYear());
  31              this.setMonth(tmp.getMonth());
  32              this.setDate(tmp.getDate());
  33          }
  34      };
  35  
  36      /** Sets the full date for the current date. */
  37      Date.prototype.setLocalDate = function (dateType, d) {
  38          if (dateType != 'gregorian') {
  39              /** Modify to match the current calendar when overriding **/
  40              return '';
  41          } else {
  42              return this.setDate(d);
  43          }
  44      };
  45  
  46      /** Sets the month for the current date. */
  47      Date.prototype.setLocalMonth = function (dateType, m, d) {
  48          if (dateType != 'gregorian') {
  49              /** Modify to match the current calendar when overriding **/
  50              return '';
  51          } else {
  52              if (d == undefined) this.getDate();
  53              return this.setMonth(m);
  54          }
  55      };
  56  
  57      /** Sets the year for the current date. */
  58      Date.prototype.setOtherFullYear = function(dateType, y) {
  59          if (dateType != 'gregorian') {
  60              /** Modify to match the current calendar when overriding **/
  61              return '';
  62          } else {
  63              var date = new Date(this);
  64              date.setFullYear(y);
  65              if (date.getMonth() != this.getMonth()) this.setDate(28);
  66              return this.setUTCFullYear(y);
  67          }
  68      };
  69  
  70      /** Sets the year for the current date. */
  71      Date.prototype.setLocalFullYear = function (dateType, y) {
  72          if (dateType != 'gregorian') {
  73              /** Modify to match the current calendar when overriding **/
  74              return '';
  75          } else {
  76              var date = new Date(this);
  77              date.setFullYear(y);
  78              if (date.getMonth() != this.getMonth()) this.setDate(28);
  79              return this.setFullYear(y);
  80          }
  81      };
  82  
  83      /********************** *************************/
  84      /**************** GETTERS ***********************/
  85      /********************** *************************/
  86  
  87      /** The number of days per week **/
  88      Date.prototype.getLocalWeekDays = function (dateType, y) {
  89          if (dateType != 'gregorian') {
  90              /** Modify to match the current calendar when overriding **/
  91              return 6;
  92          } else {
  93              return 6; // 7 days per week
  94          }
  95      };
  96  
  97      /** Returns the year for the current date. */
  98      Date.prototype.getOtherFullYear = function (dateType) {
  99          if (dateType != 'gregorian') {
 100              /** Modify to match the current calendar when overriding **/
 101              return '';
 102          } else {
 103              return this.getFullYear();
 104          }
 105      };
 106  
 107      /** Returns the year for the current date. */
 108      Date.prototype.getLocalFullYear = function (dateType) {
 109          if (dateType != 'gregorian') {
 110              /** Modify to match the current calendar when overriding **/
 111              return '';
 112          } else {
 113              return this.getFullYear();
 114          }
 115      };
 116  
 117      /** Returns the month the date. */
 118      Date.prototype.getLocalMonth = function (dateType) {
 119          if (dateType != 'gregorian') {
 120              /** Modify to match the current calendar when overriding **/
 121              return '';
 122          } else {
 123              return this.getMonth();
 124          }
 125      };
 126  
 127      /** Returns the date. */
 128      Date.prototype.getLocalDate = function (dateType) {
 129          if (dateType != 'gregorian') {
 130              /** Modify to match the current calendar when overriding **/
 131              return '';
 132          } else {
 133              return this.getDate();
 134          }
 135      };
 136  
 137      /** Returns the number of day in the year. */
 138      Date.prototype.getLocalDay = function(dateType) {
 139          if (dateType  != 'gregorian') {
 140              return '';
 141          } else {
 142              return this.getDay();
 143          }
 144      };
 145  
 146      /** Returns the number of days in the current month */
 147      Date.prototype.getLocalMonthDays = function(dateType, month) {
 148          if (dateType != 'gregorian') {
 149              /** Modify to match the current calendar when overriding **/
 150              return '';
 151          } else {
 152              var year = this.getFullYear();
 153              if (typeof month == "undefined") {
 154                  month = this.getMonth();
 155              }
 156              if (((0 == (year%4)) && ( (0 != (year%100)) || (0 == (year%400)))) && month == 1) {
 157                  return 29;
 158              } else {
 159                  return [31,28,31,30,31,30,31,31,30,31,30,31][month];
 160              }
 161          }
 162      };
 163  
 164      /** Returns the week number for the current date. */
 165      Date.prototype.getLocalWeekNumber = function(dateType) {
 166          if (dateType != 'gregorian') {
 167              /** Modify to match the current calendar when overriding **/
 168              return '';
 169          } else {
 170              var d = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
 171              var DoW = d.getDay();
 172              d.setDate(d.getDate() - (DoW + 6) % 7 + 3);                                     // Nearest Thu
 173              var ms = d.valueOf();                                                           // GMT
 174              d.setMonth(0);
 175              d.setDate(4);                                                                   // Thu in Week 1
 176              return Math.round((ms - d.valueOf()) / (7 * 864e5)) + 1;
 177          }
 178      };
 179  
 180      /** Returns the number of day in the year. */
 181      Date.prototype.getLocalDayOfYear = function(dateType) {
 182          if (dateType  != 'gregorian') {
 183              return '';
 184          } else {
 185              var now = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
 186              var then = new Date(this.getFullYear(), 0, 0, 0, 0, 0);
 187              var time = now - then;
 188              return Math.floor(time / Date.DAY);
 189          }
 190      };
 191  
 192      /** Checks date and time equality */
 193      Date.prototype.equalsTo = function(date) {
 194          return ((this.getFullYear() == date.getFullYear()) &&
 195          (this.getMonth() == date.getMonth()) &&
 196          (this.getDate() == date.getDate()) &&
 197          (this.getHours() == date.getHours()) &&
 198          (this.getMinutes() == date.getMinutes()));
 199      };
 200  
 201      /** Converts foreign date to gregorian date. */
 202      Date.localCalToGregorian = function(y, m, d) {
 203          /** Modify to match the current calendar when overriding **/
 204          return'';
 205      };
 206  
 207      /** Converts gregorian date to foreign date. */
 208      Date.gregorianToLocalCal = function(y, m, d) {
 209          /** Modify to match the current calendar when overriding **/
 210          return '';
 211      };
 212  
 213      /** Method to convert numbers to local symbols. */
 214      Date.convertNumbers = function(str) {
 215          str = str.toString();
 216  
 217          for (var i = 0, l = localNumbers.length; i < l; i++) {
 218              str = str.replace(new RegExp(i, 'g'), localNumbers[i]);
 219          }
 220  
 221          return str;
 222      };
 223  
 224      /** Translates to english numbers a string. */
 225      Date.toEnglish = function(str) {
 226          str = this.toString();
 227          var nums = [0,1,2,3,4,5,6,7,8,9];
 228          for (var i = 0; i < 10; i++) {
 229              str = str.replace(new RegExp(nums[i], 'g'), i);
 230          }
 231          return str;
 232      };
 233  
 234      /** Order the months from Gergorian to the calendar order */
 235      Date.monthsToLocalOrder = function(months) {
 236          return months;
 237      };
 238  
 239      /** INTERFACE METHODS FOR THE CALENDAR PICKER **/
 240      /************* END **************/
 241  
 242      /** Method to parse a string and return a date. **/
 243      Date.parseFieldDate = function(str, fmt, dateType, localStrings) {
 244          if (dateType != 'gregorian')
 245              str = Date.toEnglish(str);
 246  
 247          var today = new Date();
 248          var y = 0;
 249          var m = -1;
 250          var d = 0;
 251          var a = str.split(/\W+/);
 252          var b = fmt.match(/%./g);
 253          var i = 0, j = 0;
 254          var hr = 0;
 255          var min = 0;
 256          var sec = 0;
 257          for (i = 0; i < a.length; ++i) {
 258              if (!a[i])
 259                  continue;
 260              switch (b[i]) {
 261                  case "%d":
 262                  case "%e":
 263                      d = parseInt(a[i], 10);
 264                      break;
 265  
 266                  case "%m":
 267                      m = parseInt(a[i], 10) - 1;
 268                      break;
 269  
 270                  case "%Y":
 271                  case "%y":
 272                      y = parseInt(a[i], 10);
 273                      (y < 100) && (y += (y > 29) ? 1900 : 2000);
 274                      break;
 275  
 276                  case "%b":
 277                  case "%B":
 278                      for (j = 0; j < 12; ++j) {
 279                          if (localStrings.months[j].substr(0, a[i].length).toLowerCase() === a[i].toLowerCase()) {
 280                              m = j;
 281                              break;
 282                          }
 283                      }
 284                      break;
 285  
 286                  case "%H":
 287                  case "%I":
 288                  case "%k":
 289                  case "%l":
 290                      hr = parseInt(a[i], 10);
 291                      break;
 292  
 293                  case "%P":
 294                  case "%p":
 295                      if (/pm/i.test(a[i]) && hr < 12)
 296                          hr += 12;
 297                      else if (/am/i.test(a[i]) && hr >= 12)
 298                          hr -= 12;
 299                      break;
 300  
 301                  case "%M":
 302                      min = parseInt(a[i], 10);
 303                      break;
 304                  case "%S":
 305                      sec = parseInt(a[i], 10);
 306                      break;
 307              }
 308          }
 309          if (isNaN(y)) y = today.getFullYear();
 310          if (isNaN(m)) m = today.getMonth();
 311          if (isNaN(d)) d = today.getDate();
 312          if (isNaN(hr)) hr = today.getHours();
 313          if (isNaN(min)) min = today.getMinutes();
 314          if (isNaN(sec)) sec = today.getSeconds();
 315          if (y != 0 && m != -1 && d != 0)
 316              return new Date(y, m, d, hr, min, sec);
 317          y = 0; m = -1; d = 0;
 318          for (i = 0; i < a.length; ++i) {
 319              if (a[i].search(/[a-zA-Z]+/) != -1) {
 320                  var t = -1;
 321                  for (j = 0; j < 12; ++j) {
 322                      if (localStrings.months[j].substr(0, a[i].length).toLowerCase() === a[i].toLowerCase()) {
 323                          t = j;
 324                          break;
 325                      }
 326                  }
 327                  if (t != -1) {
 328                      if (m != -1) {
 329                          d = m+1;
 330                      }
 331                      m = t;
 332                  }
 333              } else if (parseInt(a[i], 10) <= 12 && m == -1) {
 334                  m = a[i]-1;
 335              } else if (parseInt(a[i], 10) > 31 && y == 0) {
 336                  y = parseInt(a[i], 10);
 337                  (y < 100) && (y += (y > 29) ? 1900 : 2000);
 338              } else if (d == 0) {
 339                  d = a[i];
 340              }
 341          }
 342          if (y == 0)
 343              y = today.getFullYear();
 344          if (m != -1 && d != 0)
 345              return new Date(y, m, d, hr, min, sec);
 346          return today;
 347      };
 348  
 349      /** Prints the date in a string according to the given format. */
 350      Date.prototype.print = function (str, dateType, translate, localStrings) {
 351          /** Handle calendar type **/
 352          if (typeof dateType !== 'string') str = '';
 353          if (!dateType) dateType = 'gregorian';
 354  
 355          /** Handle wrong format **/
 356          if (typeof str !== 'string') str = '';
 357          if (!str) return '';
 358  
 359          if (this.getLocalDate(dateType) == 'NaN' || !this.getLocalDate(dateType)) return '';
 360          var m = this.getLocalMonth(dateType);
 361          var d = this.getLocalDate(dateType);
 362          var y = this.getLocalFullYear(dateType);
 363          var wn = this.getLocalWeekNumber(dateType);
 364          var w = this.getDay();
 365          var s = {};
 366          var hr = this.getHours();
 367          var pm = (hr >= 12);
 368          var ir = (pm) ? (hr - 12) : hr;
 369          var dy = this.getLocalDayOfYear(dateType);
 370          if (ir == 0)
 371              ir = 12;
 372          var min = this.getMinutes();
 373          var sec = this.getSeconds();
 374          s["%a"] = localStrings.shortDays[w];                                                        // abbreviated weekday name
 375          s["%A"] = localStrings.days[w];                                                             // full weekday name
 376          s["%b"] = localStrings.shortMonths[m];                                                      // abbreviated month name
 377          s["%B"] = localStrings.months[m];                                                           // full month name
 378          // FIXME: %c : preferred date and time representation for the current locale
 379          s["%C"] = 1 + Math.floor(y / 100);                                                          // the century number
 380          s["%d"] = (d < 10) ? ("0" + d) : d;                                                         // the day of the month (range 01 to 31)
 381          s["%e"] = d;                                                                                // the day of the month (range 1 to 31)
 382          // FIXME: %D : american date style: %m/%d/%y
 383          // FIXME: %E, %F, %G, %g, %h (man strftime)
 384          s["%H"] = (hr < 10) ? ("0" + hr) : hr;                                                      // hour, range 00 to 23 (24h format)
 385          s["%I"] = (ir < 10) ? ("0" + ir) : ir;                                                      // hour, range 01 to 12 (12h format)
 386          s["%j"] = (dy < 100) ? ((dy < 10) ? ("00" + dy) : ("0" + dy)) : dy;                         // day of the year (range 001 to 366)
 387          s["%k"] = hr;                                                                               // hour, range 0 to 23 (24h format)
 388          s["%l"] = ir;                                                                               // hour, range 1 to 12 (12h format)
 389          s["%m"] = (m < 9) ? ("0" + (1+m)) : (1+m);                                                  // month, range 01 to 12
 390          s["%M"] = (min < 10) ? ("0" + min) : min;                                                   // minute, range 00 to 59
 391          s["%n"] = "\n";                                                                             // a newline character
 392          s["%p"] = pm ? localStrings.pm.toUpperCase() : localStrings.am.toUpperCase();
 393          s["%P"] = pm ? localStrings.pm : localStrings.am;
 394          // FIXME: %r : the time in am/pm notation %I:%M:%S %p
 395          // FIXME: %R : the time in 24-hour notation %H:%M
 396          s["%s"] = Math.floor(this.getTime() / 1000);
 397          s["%S"] = (sec < 10) ? ("0" + sec) : sec;                                                   // seconds, range 00 to 59
 398          s["%t"] = "\t";                                                                             // a tab character
 399          // FIXME: %T : the time in 24-hour notation (%H:%M:%S)
 400          s["%U"] = s["%W"] = s["%V"] = (wn < 10) ? ("0" + wn) : wn;
 401          s["%u"] = w + 1;                                                                            // the day of the week (range 1 to 7, 1 = MON)
 402          s["%w"] = w;                                                                                // the day of the week (range 0 to 6, 0 = SUN)
 403          // FIXME: %x : preferred date representation for the current locale without the time
 404          // FIXME: %X : preferred time representation for the current locale without the date
 405          s["%y"] = ('' + y).substr(2, 2);                                                            // year without the century (range 00 to 99)
 406          s["%Y"] = y;                                                                                // year with the century
 407          s["%%"] = "%";                                                                              // a literal '%' character
 408  
 409          var re = /%./g;
 410  
 411          var tmpDate = str.replace(re, function (par) { return s[par] || par; });
 412          if (dateType != 'gregorian' && translate) {
 413              tmpDate = Date.convertNumbers(tmpDate);
 414          }
 415  
 416          return tmpDate;
 417      };
 418  })(Date);


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer