Home  >  Article  >  Web Front-end  >  Time format processing function Date.prototype.format_javascript skills under JavaScript

Time format processing function Date.prototype.format_javascript skills under JavaScript

WBOY
WBOYOriginal
2016-05-16 15:17:331706browse

实例一:

一个全的js时间处理函数,虽然我没有仔细去研究里面的正则,但是我经过了测试,是非常好用的,你可以根据你自己的需求设置想要的时间格式的字符串输出,我应用到的格式为:MM/dd/yyyy hh:mm TT和yyyy-MM-dd HH:mm:ss。

<span style="font-size:18px;">Date.prototype.format = function (mask) 
{ 
var d = this; 
var zeroize = function (value, length) 
{ 
if (!length) length = 2; 
value = String(value); 
for (var i = 0, zeros = ''; i < (length - value.length); i++) 
{ 
zeros += '0'; 
} 
return zeros + value; 
}; 
return mask.replace(/"[^"]*"|'[^']*'|\b(&#63;:d{1,4}|m{1,4}|yy(&#63;:yy)&#63;|([hHMstT])\1&#63;|[lLZ])\b/g, function ($0) 
{ 
switch ($0) 
{ 
case 'd': return d.getDate(); 
case 'dd': return zeroize(d.getDate()); 
case 'ddd': return ['Sun', 'Mon', 'Tue', 'Wed', 'Thr', 'Fri', 'Sat'][d.getDay()]; 
case 'dddd': return ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'][d.getDay()]; 
case 'M': return d.getMonth() + 1; 
case 'MM': return zeroize(d.getMonth() + 1); 
case 'MMM': return ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][d.getMonth()]; 
case 'MMMM': return ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'][d.getMonth()]; 
case 'yy': return String(d.getFullYear()).substr(2); 
case 'yyyy': return d.getFullYear(); 
case 'h': return d.getHours() % 12 || 12; 
case 'hh': return zeroize(d.getHours() % 12 || 12); 
case 'H': return d.getHours(); 
case 'HH': return zeroize(d.getHours()); 
case 'm': return d.getMinutes(); 
case 'mm': return zeroize(d.getMinutes()); 
case 's': return d.getSeconds(); 
case 'ss': return zeroize(d.getSeconds()); 
case 'l': return zeroize(d.getMilliseconds(), 3); 
case 'L': var m = d.getMilliseconds(); 
if (m > 99) m = Math.round(m / 10); 
return zeroize(m); 
case 'tt': return d.getHours() < 12 &#63; 'am' : 'pm'; 
case 'TT': return d.getHours() < 12 &#63; 'AM' : 'PM'; 
case 'Z': return d.toUTCString().match(/[A-Z]+$/); 
// Return quoted strings with the surrounding quotes removed 
default: return $0.substr(1, $0.length - 2); 
} 
}); 
};</span> 

实例二:

Date.prototype.format方法在date的原型中扩展了format方法,使其可以方便的格式化日期格式输出。

Date.prototype.format =function(format)
{
var o = {
"M+" : this.getMonth()+1, //month
"d+" : this.getDate(), //day
"h+" : this.getHours(), //hour
"m+" : this.getMinutes(), //minute
"s+" : this.getSeconds(), //second
"q+" : Math.floor((this.getMonth()+3)/3), //quarter
"S" : this.getMilliseconds() //millisecond
}
if(/(y+)/.test(format)) format=format.replace(RegExp.$1,
(this.getFullYear()+"").substr(4- RegExp.$1.length));
for(var k in o)if(new RegExp("("+ k +")").test(format))
format = format.replace(RegExp.$1,
RegExp.$1.length==1&#63; o[k] :
("00"+ o[k]).substr((""+ o[k]).length));
return format;
}
var date = new Date(parseInt("1347497754133"));
date.format("yyyy-MM-dd");

以上通过两段实例代码给大家介绍了JavaScript下的时间格式处理函数Date.prototype.format的相关内容,希望对大家有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn