欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

javascript format Date 简单的时间格式化

程序员文章站 2022-07-15 07:52:40
...
<script type="text/javascript">
var formatDate = function (formatDate, formatString) {
if(formatDate instanceof Date) {
var months = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var yyyy = formatDate.getFullYear();
var yy = yyyy.toString().substring(2);
var m = formatDate.getMonth();
var mm = m < 10 ? "0" + m : m;
var mmm = months[m];
var d = formatDate.getDate();
var dd = d < 10 ? "0" + d : d;

var h = formatDate.getHours();
var hh = h < 10 ? "0" + h : h;
var n = formatDate.getMinutes();
var nn = n < 10 ? "0" + n : n;
var s = formatDate.getSeconds();
var ss = s < 10 ? "0" + s : s;

formatString = formatString.replace(/yyyy/i, yyyy);
formatString = formatString.replace(/yy/i, yy);
formatString = formatString.replace(/mmm/i, mmm);
formatString = formatString.replace(/mm/i, mm);
formatString = formatString.replace(/m/i, m);
formatString = formatString.replace(/dd/i, dd);
formatString = formatString.replace(/d/i, d);
formatString = formatString.replace(/hh/i, hh);
formatString = formatString.replace(/h/i, h);
formatString = formatString.replace(/nn/i, nn);
formatString = formatString.replace(/n/i, n);
formatString = formatString.replace(/ss/i, ss);
formatString = formatString.replace(/s/i, s);

return formatString;
} else {
return "";
}

}
</pre>

This will show a demo date

<pre lang="javascript">
alert(formatDate(new Date(), "d mmm yyyy hh:nn:ss"));
相关标签: JavaScript