Posted By

kif on 07/27/06


Tagged

date format


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

LeeRJohnson
kele5240


Date Extend


 / Published in: JavaScript
 

Support: Fx2, Opera9, IE6 Date.getFormatTime() can get time by the method of like PHP date(). The code supports English and Japanese. Of course, it is also easy to add your language.

  1. /*-----------------------------------------------------------------------------
  2.  * Date Extend
  3.  * Example Source
  4. Date.UTC = true;
  5. document.write(Date.getFormatTime('%Y-%M-%DT%H:%M:%S%Z') + '<br>');
  6. var p = new Date();
  7. p.UTC = false;
  8. p.setFormatTime('2001-08-02T10:45:23.5');
  9. p.setLocale('ja');
  10. document.write(p.getFormatTime());
  11.  *-------------------------------------------------------------------------- */
  12.  
  13. Date.isLeapYear = function (year) {
  14. return (
  15. ((year % 400) == 0) ? 1 :
  16. ((year % 100) == 0) ? 0 :
  17. ((year % 4) == 0) ? 1 :
  18. 0
  19. );
  20. }
  21.  
  22. Date.prototype.isLeapYear = function () {
  23. return Date.isLeapYear((this.utc) ? this.getUTCFullYear() : this.getFullYear());
  24. }
  25.  
  26. /*
  27.  * Local
  28.  */
  29.  
  30. Date._locale = {
  31. en: {
  32. n: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
  33. N: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
  34. l: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
  35. L: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
  36. a: ['am', 'pm'],
  37. A: ['AM', 'PM'],
  38. format: '%l, %D %n %Y %H:%I:%S'
  39. },
  40. ja: {
  41. n: [' 1\u6708', ' 2\u6708', ' 3\u6708', ' 4\u6708', ' 5\u6708', ' 6\u6708', ' 7\u6708', ' 8\u6708', ' 9\u6708', '10\u6708', '11\u6708', '12\u6708'],
  42. N: ['1\u6708', '2\u6708', '3\u6708', '4\u6708', '5\u6708', '6\u6708', '7\u6708', '8\u6708', '9\u6708', '10\u6708', '11\u6708', '12\u6708'],
  43. l: ['\u65E5', '\u6708', '\u706B', '\u6C34', '\u6728', '\u91D1', '\u571F'],
  44. L: ['\u65E5\u66DC\u65E5', '\u6708\u66DC\u65E5', '\u706B\u66DC\u65E5', '\u6C34\u66DC\u65E5', '\u6728\u66DC\u65E5', '\u91D1\u66DC\u65E5', '\u571F\u66DC\u65E5'],
  45. a: ['AM', 'PM'],
  46. A: ['\u5348\u524D', '\u5348\u5F8C'],
  47. format: '%Y\u5E74%m\u6708%d\u65E5 %A%g\u6642%i\u5206%s\u79D2'
  48. }
  49. }
  50.  
  51. Date.UTC = false;
  52. Date.locale = 'en';
  53.  
  54. Date.setLocale = function (lc) {
  55. Date.locale = lc || 'en';
  56. }
  57.  
  58. Date.prototype.setLocale = function (lc) {
  59. this.locale = lc;
  60. return this;
  61. }
  62.  
  63. /*
  64.  * Format
  65.  * Year ----- %y (2), %Y (4)
  66.  * Month ---- %m (1), %M (2), %n (ry), %N (Full)
  67.  * Date ----- %d (1), %D (2)
  68.  * Day ------ %l (ry), %L (Full), %w
  69.  * Hour ----- %h (1), %H (2) (24), %g (1), %G (2) (12)
  70.  * Minite --- %i (1), %I (2)
  71.  * Second --- %s (1), %S (2)
  72.  * AM, PM --- %a, %A
  73.  * TimeZone - %z (Min), %Z (+/-HH:MM)
  74.  * Beet ----- %B
  75.  */
  76.  
  77. Date.getFormatTime = function (format) {
  78. return (new Date()).getFormatTime(format);
  79. }
  80.  
  81. Date.prototype.getFormatTime = function (format) {
  82. var locale = Date._locale[this.locale || Date.locale];
  83. format = format || locale['format'];
  84. if (this.UTC != true && this.UTC != false)
  85. this.UTC = Date.UTC;
  86. var y = (this.UTC) ? this.getUTCFullYear() : this.getFullYear(),
  87. m = (this.UTC) ? this.getUTCMonth() : this.getMonth(),
  88. d = (this.UTC) ? this.getUTCDate() : this.getDate(),
  89. w = (this.UTC) ? this.getUTCDay() : this.getDay(),
  90. h = (this.UTC) ? this.getUTCHours() : this.getHours(),
  91. i = (this.UTC) ? this.getUTCMinutes() : this.getMinutes(),
  92. s = (this.UTC) ? this.getUTCSeconds() : this.getSeconds(),
  93. w = (this.UTC) ? this.getUTCDay() : this.getDay()
  94. z = this.getTimezoneOffset();
  95. return format.replaceMulti({
  96. '%y': ('' + y).slice(2),
  97. '%Y': y,
  98. '%m': m + 1,
  99. '%M': (m + 1).zeropad(2),
  100. '%n': locale['n'][m],
  101. '%N': locale['N'][m],
  102. '%d': d,
  103. '%D': d.zeropad(2),
  104. '%l': locale['l'][w],
  105. '%L': locale['L'][w],
  106. '%w': w,
  107. '%h': h,
  108. '%H': h.zeropad(2),
  109. '%g': h % 12 || 12,
  110. '%G': (h % 12 || 12).zeropad(2),
  111. '%i': i,
  112. '%I': i.zeropad(2),
  113. '%s': s,
  114. '%S': s.zeropad(2),
  115. '%a': locale['a'][Math.floor(h / 12 || 1)],
  116. '%A': locale['A'][Math.floor(h / 12 || 1)],
  117. '%z': z,
  118. '%Z': (z *= -1, ((z < 0) ? '' : '+') + Math.floor(z / 60).zeropad(2) + ':' + (z % 60).zeropad(2)),
  119. '%B': this.getInternetTime()
  120. }, 'g');
  121. }
  122.  
  123. Date.getInternetTime = function () {
  124. return (new Date()).getInternetTime();
  125. }
  126.  
  127. Date.prototype.getInternetTime = function () {
  128. var t = this.getUTCHours() + 1;
  129. return Math.floor((((t > 23) ? 0 : t * 3600) + this.getUTCMinutes() * 60 + this.getUTCSeconds()) / 86.4).zeropad(3);
  130. }
  131.  
  132. /*
  133.  * Format
  134.  * Year
  135.  * YYYY (ex: 2001)
  136.  * Year Month
  137.  * YYYY-MM (ex: 2001-08)
  138.  * Year Month Day
  139.  * YYYY-MM-DD (ex: 2001-08-02)
  140.  * Year Month Day Hour Minite
  141.  * YYYY-MM-DDThh:mm (ex: 2001-08-02T10:45)
  142.  * Year Month Day Hour Minite Second
  143.  * YYYY-MM-DDThh:mm:ss (ex: 2001-08-02T10:45:23)
  144.  * Year Month Day Hour Minite Second Millisecond
  145.  * YYYY-MM-DDThh:mm:ss.s (ex: 2001-08-02T10:45:23.5)
  146.  */
  147.  
  148. Date.prototype.setFormatTime = function (format) {
  149. var tmp = format.split('T');
  150. var date = tmp[0].split('-');
  151. this.setFullYear(parseInt(date[0] || 0, 10));
  152. this.setMonth(parseInt(date[1] || 1, 10) - 1);
  153. this.setDate(parseInt(date[2] || 1, 10));
  154. if (tmp[1]) {
  155. var time = tmp[1].split(':');
  156. this.setHours(parseInt(time[0] || 0, 10));
  157. this.setMinutes(parseInt(time[1] || 0, 10));
  158. this.setSeconds(Math.floor(parseInt(time[2] || 0, 10)));
  159. this.setMilliseconds(parseInt(time[2] || 0, 10) * 1000 % 1000);
  160. }
  161. }
  162.  
  163. Date.prototype.setInternetTime = function (beet) {
  164. var t = parseInt(beet), p;
  165. t = ((t < 0) ? 0 : (t > 999) ? 999 : t) * 86.4;
  166. this.setUTCHours(((p = Math.floor(t / 3600)) == 0) ? 23 : p - 1);
  167. t -= (p * 3600);
  168. this.setUTCMinutes(p = Math.floor(t / 60));
  169. this.setUTCSeconds(t - (p * 60));
  170. }
  171.  
  172. /*
  173.  * Extend
  174.  */
  175.  
  176. String.prototype.replaceMulti = function (p, o) {
  177. var t = this;
  178. if (!o) o = '';
  179. for (var i in p)
  180. t = t.replace(new RegExp(i, o), p[i]);
  181. return t;
  182. }
  183.  
  184. Number.prototype.zeropad = function (c) {
  185. return (
  186. (this < 0) ? '-' + (this * -1).zeropad(c) :
  187. (('' + this).length >= c) ? this :
  188. ('0'.repeat(c) + this).slice(-c)
  189. );
  190. }

Report this snippet  

You need to login to post a comment.