/ 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.
Expand |
Embed | Plain Text
/*----------------------------------------------------------------------------- * Date Extend * Example Source Date.UTC = true; document.write(Date.getFormatTime('%Y-%M-%DT%H:%M:%S%Z') + '<br>'); var p = new Date(); p.UTC = false; p.setFormatTime('2001-08-02T10:45:23.5'); p.setLocale('ja'); document.write(p.getFormatTime()); *-------------------------------------------------------------------------- */ Date.isLeapYear = function (year) { return ( ((year % 400) == 0) ? 1 : ((year % 100) == 0) ? 0 : ((year % 4) == 0) ? 1 : 0 ); } Date.prototype.isLeapYear = function () { return Date.isLeapYear((this.utc) ? this.getUTCFullYear() : this.getFullYear()); } /* * Local */ Date._locale = { en: { n: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], N: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'], l: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], L: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], a: ['am', 'pm'], A: ['AM', 'PM'], format: '%l, %D %n %Y %H:%I:%S' }, ja: { 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'], 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'], l: ['\u65E5', '\u6708', '\u706B', '\u6C34', '\u6728', '\u91D1', '\u571F'], L: ['\u65E5\u66DC\u65E5', '\u6708\u66DC\u65E5', '\u706B\u66DC\u65E5', '\u6C34\u66DC\u65E5', '\u6728\u66DC\u65E5', '\u91D1\u66DC\u65E5', '\u571F\u66DC\u65E5'], a: ['AM', 'PM'], A: ['\u5348\u524D', '\u5348\u5F8C'], format: '%Y\u5E74%m\u6708%d\u65E5 %A%g\u6642%i\u5206%s\u79D2' } } Date.UTC = false; Date.locale = 'en'; Date.setLocale = function (lc) { Date.locale = lc || 'en'; } Date.prototype.setLocale = function (lc) { this.locale = lc; return this; } /* * Format * Year ----- %y (2), %Y (4) * Month ---- %m (1), %M (2), %n (ry), %N (Full) * Date ----- %d (1), %D (2) * Day ------ %l (ry), %L (Full), %w * Hour ----- %h (1), %H (2) (24), %g (1), %G (2) (12) * Minite --- %i (1), %I (2) * Second --- %s (1), %S (2) * AM, PM --- %a, %A * TimeZone - %z (Min), %Z (+/-HH:MM) * Beet ----- %B */ Date.getFormatTime = function (format) { return (new Date()).getFormatTime(format); } Date.prototype.getFormatTime = function (format) { var locale = Date._locale[this.locale || Date.locale]; format = format || locale['format']; if (this.UTC != true && this.UTC != false) this.UTC = Date.UTC; var y = (this.UTC) ? this.getUTCFullYear() : this.getFullYear(), m = (this.UTC) ? this.getUTCMonth() : this.getMonth(), d = (this.UTC) ? this.getUTCDate() : this.getDate(), w = (this.UTC) ? this.getUTCDay() : this.getDay(), h = (this.UTC) ? this.getUTCHours() : this.getHours(), i = (this.UTC) ? this.getUTCMinutes() : this.getMinutes(), s = (this.UTC) ? this.getUTCSeconds() : this.getSeconds(), w = (this.UTC) ? this.getUTCDay() : this.getDay() z = this.getTimezoneOffset(); return format.replaceMulti({ '%y': ('' + y).slice(2), '%Y': y, '%m': m + 1, '%M': (m + 1).zeropad(2), '%n': locale['n'][m], '%N': locale['N'][m], '%d': d, '%D': d.zeropad(2), '%l': locale['l'][w], '%L': locale['L'][w], '%w': w, '%h': h, '%H': h.zeropad(2), '%g': h % 12 || 12, '%G': (h % 12 || 12).zeropad(2), '%i': i, '%I': i.zeropad(2), '%s': s, '%S': s.zeropad(2), '%a': locale['a'][Math.floor(h / 12 || 1)], '%A': locale['A'][Math.floor(h / 12 || 1)], '%z': z, '%Z': (z *= -1, ((z < 0) ? '' : '+') + Math.floor(z / 60).zeropad(2) + ':' + (z % 60).zeropad(2)), '%B': this.getInternetTime() }, 'g'); } Date.getInternetTime = function () { return (new Date()).getInternetTime(); } Date.prototype.getInternetTime = function () { var t = this.getUTCHours() + 1; return Math.floor((((t > 23) ? 0 : t * 3600) + this.getUTCMinutes() * 60 + this.getUTCSeconds()) / 86.4).zeropad(3); } /* * Format * Year * YYYY (ex: 2001) * Year Month * YYYY-MM (ex: 2001-08) * Year Month Day * YYYY-MM-DD (ex: 2001-08-02) * Year Month Day Hour Minite * YYYY-MM-DDThh:mm (ex: 2001-08-02T10:45) * Year Month Day Hour Minite Second * YYYY-MM-DDThh:mm:ss (ex: 2001-08-02T10:45:23) * Year Month Day Hour Minite Second Millisecond * YYYY-MM-DDThh:mm:ss.s (ex: 2001-08-02T10:45:23.5) */ Date.prototype.setFormatTime = function (format) { var tmp = format.split('T'); var date = tmp[0].split('-'); this.setFullYear(parseInt(date[0] || 0, 10)); this.setMonth(parseInt(date[1] || 1, 10) - 1); this.setDate(parseInt(date[2] || 1, 10)); if (tmp[1]) { var time = tmp[1].split(':'); this.setHours(parseInt(time[0] || 0, 10)); this.setMinutes(parseInt(time[1] || 0, 10)); this.setSeconds(Math.floor(parseInt(time[2] || 0, 10))); this.setMilliseconds(parseInt(time[2] || 0, 10) * 1000 % 1000); } } Date.prototype.setInternetTime = function (beet) { var t = parseInt(beet), p; t = ((t < 0) ? 0 : (t > 999) ? 999 : t) * 86.4; this.setUTCHours(((p = Math.floor(t / 3600)) == 0) ? 23 : p - 1); t -= (p * 3600); this.setUTCMinutes(p = Math.floor(t / 60)); this.setUTCSeconds(t - (p * 60)); } /* * Extend */ String.prototype.replaceMulti = function (p, o) { var t = this; if (!o) o = ''; for (var i in p) t = t.replace(new RegExp(i, o), p[i]); return t; } Number.prototype.zeropad = function (c) { return ( (this < 0) ? '-' + (this * -1).zeropad(c) : (('' + this).length >= c) ? this : ('0'.repeat(c) + this).slice(-c) ); }
You need to login to post a comment.
