Js 日期格式化

/***
** HongShijin
** [email protected]
** 2014-10-15 9:13:00.00000
** text/javascript
***/

(function ($)
{
function DF(format)//SimpleDateFormat
{
this.input = format;
this.Y = 0;
this.y = 0;
this.m = 0;
this.d = 0;
this.H = 0;
this.h = 0;
this.n = 0;
this.s = 0;
this.f = 0;
this.w = 0;
this.z = 0;
}

DF.mmm = [‘Jan‘, ‘Feb‘, ‘Mar‘, ‘Apr‘, ‘May‘, ‘Jun‘, ‘Jul‘, ‘Aug‘, ‘Sep‘, ‘Oct‘, ‘Nov‘, ‘Dec‘];
DF.prototype.mmm = DF.mmm;
DF.mmmm = [‘January‘, ‘February‘, ‘March‘, ‘April‘, ‘May‘, ‘June‘, ‘July‘, ‘August‘, ‘September‘, ‘October‘, ‘November‘, ‘December‘];
DF.prototype.mmmm = DF.mmmm;
DF.T = [String.fromCharCode(0x3007), String.fromCharCode(0x4E00), String.fromCharCode(0x4E8C), String.fromCharCode(0x4E09), String.fromCharCode(0x56DB), String.fromCharCode(0x4E94), String.fromCharCode(0x516D), String.fromCharCode(0x4E03), String.fromCharCode(0x516B), String.fromCharCode(0x4E5D), String.fromCharCode(0x5341)]; // 〇一二三四五六七八九十
DF.prototype.T = DF.T;
DF.W = [String.fromCharCode(0x65E5), DF.T[1], DF.T[2], DF.T[3], DF.T[4], DF.T[5], DF.T[6], DF.T[7], DF.T[8], DF.T[9]]; // 日一二三四五六七八九十
DF.prototype.W = DF.W;
DF.WW = [‘Sun‘, ‘Mon‘, ‘Tue‘, ‘Wed‘, ‘Thr‘, ‘Fri‘, ‘Sat‘];
DF.prototype.WW = DF.WW;
DF.WWW = [‘Sunday‘, ‘Monday‘, ‘Tuesday‘, ‘Wednesday‘, ‘Thursday‘, ‘Friday‘, ‘Saturday‘];
DF.prototype.WWW = DF.WWW;

DF.prototype._YYYY = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return this.T[Math.floor(this.Y / 1000)] + this.T[Math.floor((this.Y % 1000) / 100)] + this.T[Math.floor((this.Y % 100) / 10)] + this.T[this.Y % 10];
};

DF.prototype._YY = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return this.T[Math.floor((this.y % 100) / 10)] + this.T[this.y % 10];
};

DF.prototype._Y = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return (y > 10 ? T[Math.floor((y % 100) / 10)] : ‘‘) + T[y % 10];
};

DF.prototype.__Y = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

if (match.length >= 4)
return this._YYYY(match);

if (match.length >= 2)
return this._YY(match);

return this._Y(match);
};

DF.prototype._yyyy = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return this.Y;
};

DF.prototype._yy = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return (this.y > 9 ? ‘‘ : ‘0‘) + this.y;
};

DF.prototype._y = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return this.y;
};

DF.prototype.__y = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

if (match.length >= 4)
return this._yyyy(match);

if (match.length >= 2)
return this._yy(match);

return this._y(match);
};

DF.prototype._M = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return (this.m >= 10 ? this.T[10] : ‘‘) + (this.m % 10 > 0 ? this.T[this.m % 10] : ‘‘);
};

DF.prototype.__M = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return this._M(match);
};

DF.prototype._mmmm = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return this.mmmm[this.m];
};

DF.prototype._mmm = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return this.mmm[this.m];
};

DF.prototype._mm = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return (this.m > 9 ? ‘‘ : ‘0‘) + this.m;
};

DF.prototype._m = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return this.m;
};

DF.prototype.__m = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

if (match.length >= 4)
return this._mmmm(match);

if (match.length >= 3)
return this._mmm(match);

if (match.length >= 2)
return this._mm(match);

return this._m(match);
};

DF.prototype._D = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return (this.d >= 30 ? (this.T[3] + this.T[10]) : this.d >= 20 ? (this.T[2] + this.T[10]) : this.d >= 10 ? this.T[10] : ‘‘) + (this.d % 10 > 0 ? this.T[this.d % 10] : ‘‘);
};

DF.prototype.__D = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return this._D(match);
};

DF.prototype._dd = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return (this.d > 9 ? ‘‘ : ‘0‘) + this.d;
};

DF.prototype._d = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return this.d;
};

DF.prototype.__d = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

if (match.length >= 2)
return this._dd(match);

return this._d(match);
};

DF.prototype._HH = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return (this.H > 9 ? ‘‘ : ‘0‘) + this.H;
};

DF.prototype._H = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return this.H;
};

DF.prototype.__H = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

if (match.length >= 2)
return this._HH(match);

return this._H(match);
};

DF.prototype._hh = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match;

return (this.h > 9 ? ‘‘ : ‘0‘) + this.h;
};

DF.prototype._h = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return this.h;
};

DF.prototype.__h = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

if (match.length >= 2)
return this._hh(match);

return this._h(match);
};

DF.prototype._nn = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return (this.n > 9 ? ‘‘ : ‘0‘) + this.n;
};

DF.prototype._n = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return this.n;
};

DF.prototype.__n = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

if (match.length >= 2)
return this._nn(match);

return this._n(match);
};

DF.prototype._ss = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return (this.s > 9 ? ‘‘ : ‘0‘) + this.s;
};

DF.prototype._s = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return this.s;
};

DF.prototype.__s = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

if (match.length >= 2)
return this._ss(match);

return this._s(match);
};

DF.prototype._f = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

if (!match || !match.length)
return this.f;

var v = this.f + ‘‘;
return v.substring(0, match.length);
};

DF.prototype.__f = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

return this._f(match);
};

DF.prototype._WW = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match;

return this.WWW[this.w];
};

DF.prototype._W = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match;

return this.W[this.w];
};

DF.prototype.__W = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

if (match.length >= 2)
return this._WW(match);

return this._W(match);
};

DF.prototype._ww = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match;

return this.WW[this.w];
};

DF.prototype._w = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match;

return this.w;
};

DF.prototype.__w = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

if (match.length >= 2)
return this._ww(match);

return this._w(match);
};

DF.prototype._TT = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match;

return String.fromCharCode(this.H >= 12 ? 19979 : 19978);
};

DF.prototype._T = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match;

return this.H >= 12 ? ‘P‘ : ‘A‘;
};

DF.prototype.__T = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match.substring(1, match.length);

if (match.length >= 2)
return this._TT(match);

return this._T(match);
};

DF.prototype._t = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match;

return this.H >= 12 ? ‘p‘ : ‘a‘;
};

DF.prototype.__t = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match;

return this._t(match);
};

DF.prototype._ZZ = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match;

return -this.z / 60;
};

DF.prototype._Z = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match;

return this.z / 60;
};

DF.prototype.__Z = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match;

if (match.length >= 2)
return this._ZZ(match);

return this._Z(match);
};

DF.prototype._zz = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match;

return -this.z;
};

DF.prototype._z = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match;

return this.z;
};

DF.prototype.__z = function (match)
{
if (!!match && match.substring(0, 1) == ‘\\‘)
return match;

if (match.length >= 2)
return this._zz(match);

return this._z(match);
};

DF.prototype.format = function (input)
{
var me = this;
var date = input;
format = me.input;
if (!format || format == ‘‘)//var df = new SimpleDateFormat();
format = ‘yyyy-mm-dd‘; // return this.getFullYear() + ‘-‘ + (this.getMonth() + 1) + ‘-‘ + this.getDate();

if (format == ‘G‘)
return date.toGMTString();

if (format == ‘U‘)
return date.toUTCString();

if (format == ‘UT‘)
return date.toUTCTimeString();

if (format == ‘T‘)
return date.getTime();

me.Y = date.getFullYear();
me.y = date.getYear() % 100;
me.m = date.getMonth() + 1;
me.d = date.getDate();
me.H = date.getHours();
me.h = me.H % 12 == 0 ? me.H : me.H % 12;
me.n = date.getMinutes();
me.s = date.getSeconds();
me.f = date.getMilliseconds();
me.w = date.getDay();
me.z = date.getTimezoneOffset();
format = format.replace(/\\?T+/mg, function () { return me.__T.apply(me, arguments); }); // 大写英文上下午:A
//format = format.replace(/\\?TT+/img, function () { return me._TT.apply(me, arguments); }); // 中文上下午:上
//format = format.replace(/\\?T+/mg, function () { return me._T.apply(me, arguments); }); // 大写英文上下午:A
format = format.replace(/\\?t+/mg, function () { return me.__t.apply(me, arguments); }); // 小写英文上下午:a
//format = format.replace(/\\?t+/mg, function () { return me._t.apply(me, arguments); }); // 小写英文上下午:a
format = format.replace(/\\?Z+/mg, function () { return me.__Z.apply(me, arguments); }); // 时区:8
//format = format.replace(/\\?ZZ+/mg, function () { return me._ZZ.apply(me, arguments); }); // 时区:8
//format = format.replace(/\\?Z+/mg, function () { return me._Z.apply(me, arguments); }); // 时区:8
format = format.replace(/\\?z+/mg, function () { return me.__z.apply(me, arguments); }); // 时区:480
//format = format.replace(/\\?zz+/mg, function () { return me._zz.apply(me, arguments); }); // 时区:480
//format = format.replace(/\\?z+/mg, function () { return me._z.apply(me, arguments); }); // 时区:480
format = format.replace(/\\?f+/img, function () { return me.__f.apply(me, arguments); }); // 毫秒
//format = format.replace(/\\?f+/img, function () { return me._f.apply(me, arguments); }); // 毫秒
format = format.replace(/\\?s+/img, function () { return me.__s.apply(me, arguments); }); // 独数秒:01
//format = format.replace(/\\?ss+/img, function () { return me._ss.apply(me, arguments); }); // 双数秒:01
//format = format.replace(/\\?s+/img, function () { return me._s.apply(me, arguments); }); // 独数秒:01
format = format.replace(/\\?n+/img, function () { return me.__n.apply(me, arguments); }); // 独数分钟:01
//format = format.replace(/\\?nn+/img, function () { return me._nn.apply(me, arguments); }); // 双数分钟:01
//format = format.replace(/\\?n+/img, function () { return me._n.apply(me, arguments); }); // 独数分钟:01
format = format.replace(/\\?H+/mg, function () { return me.__H.apply(me, arguments); }); // 24小时制独数小时:1
//format = format.replace(/\\?HH+/mg, function () { return me._HH.apply(me, arguments); }); // 24小时制双数小时:13
//format = format.replace(/\\?H+/mg, function () { return me._H.apply(me, arguments); }); // 24小时制独数小时:1
format = format.replace(/\\?h+/mg, function () { return me.__h.apply(me, arguments); }); // 12小时制独数小时:1
//format = format.replace(/\\?hh+/mg, function () { return me._hh.apply(me, arguments); }); // 12小时制双数小时:1
//format = format.replace(/\\?h+/mg, function () { return me._h.apply(me, arguments); }); // 12小时制独数小时:1
format = format.replace(/\\?D+/mg, function () { return me.__D.apply(me, arguments); }); // 大写日期:二十五
//format = format.replace(/\\?D+/mg, function () { return me._D.apply(me, arguments); }); // 大写日期:二十五
format = format.replace(/\\?d+/mg, function () { return me.__d.apply(me, arguments); }); // 数字双日期:01
//format = format.replace(/\\?dd+/mg, function () { return me._dd.apply(me, arguments); }); // 数字双日期:01
//format = format.replace(/\\?d+/mg, function () { return me._d.apply(me, arguments); }); // 数字独日期:1
format = format.replace(/\\?M+/mg, function () { return me.__M.apply(me, arguments); }); // 大写月:十二
//format = format.replace(/\\?M+/mg, function () { return me._M.apply(me, arguments); }); // 大写月:十二
format = format.replace(/\\?m+/mg, function () { return me.__m.apply(me, arguments); }); // 英文全名月:January
//format = format.replace(/\\?mmmm+/mg, function () { return me._mmmm.apply(me, arguments); }); // 英文全名月:January
//format = format.replace(/\\?mmm+/mg, function () { return me._mmm.apply(me, arguments); }); // 英文半名月: Jan
//format = format.replace(/\\?mm+/mg, function () { return me._mm.apply(me, arguments); }); // 数字双月:01
//format = format.replace(/\\?m+/mg, function () { return me._m.apply(me, arguments); }); // 数字独月:1
format = format.replace(/\\?Y+/mg, function () { return me.__Y.apply(me, arguments); }); // 大写全年:一九八一
//format = format.replace(/\\?YYYY+/mg, function () { return me._YYYY.apply(me, arguments); }); // 大写全年:一九八一
//format = format.replace(/\\?YY+/mg, function () { return me._YY.apply(me, arguments); }); // 大写双年:〇一
//format = format.replace(/\\?Y+/mg, function () { return me._Y.apply(me, arguments); }); // 大写独年:一
format = format.replace(/\\?y+/mg, function () { return me.__y.apply(me, arguments); }); // 数字全年:1981
//format = format.replace(/\\?yyyy+/mg, function () { return me._yyyy.apply(me, arguments); }); // 数字全年:1981
//format = format.replace(/\\?yy+/mg, function () { return me._yy.apply(me, arguments); }); // 数字双年:01
//format = format.replace(/\\?y+/mg, function () { return me._y.apply(me, arguments); }); // 数字独年:1
format = format.replace(/\\?W+/mg, function () { return me.__W.apply(me, arguments); }); // 中文周:日
//format = format.replace(/\\?WW+/mg, function () { return me._WW.apply(me, arguments); }); // 英文全周:Sunday
//format = format.replace(/\\?W+/mg, function () { return me._W.apply(me, arguments); }); // 中文周:日
format = format.replace(/\\?w+/mg, function () { return me.__w.apply(me, arguments); }); // 数字周:0
//format = format.replace(/\\?ww+/mg, function () { return me._ww.apply(me, arguments); }); // 英文半周:Sun
//format = format.replace(/\\?w+/mg, function () { return me._w.apply(me, arguments); }); // 数字周:0
return format;
};

SimpleDateFormat = DF;
Date.SimpleDateFormat = DF;
window.SimpleDateFormat = DF;
document.SimpleDateFormat = DF;

Date.prototype.toUTCTimeString = function ()
{
return new Date(this.getTime() + this.getTimezoneOffset() * 60 * 1000).toLocaleTimeString();
};

Date.prototype.toDateString = function (format)
{
var df = new Date.SimpleDateFormat(format);
return df.format(this);
};

Date.prototype.baseToString = Date.prototype.toString;

Date.prototype.toString = function (format)
{
if (!arguments.length)
return this.baseToString();

if (!format || format == ‘‘)
format = ‘yyyy-mm-dd HH:nn:ss‘;
return this.toDateString(format);
};

})(null);

/*

因为有部分浏览器不支持内置对象的方法重载,所以 toString 方法兼容性有问题,因此这里增加了 toDateString 方法。

new Date().toString(‘yyyy-mm-dd‘);//1981-03-01

new Date().toString(‘YYYY年MM月DD日‘);//一九八一年三月一日

new Date().toString(‘YYYY年MM月DD日 = \\YYYY年\\MM月\\DD日‘‘);//一九八一年三月一日 = YYYY年MM月DD日

反斜杠表示转义,表示非格式化字符串。

*/

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。