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"));
上一篇: 动态设定Spring中bean的属性
下一篇: Dozer使用总结
推荐阅读
-
PHP日期函数date格式化UNIX时间的方法,dateunix_PHP教程
-
和PHP的date函数用法一样的JS时间戳格式化函数
-
PHP日期函数date格式化UNIX时间的方法,dateunix
-
php用date格式化输出指定范围的时间
-
js实现格式化金额,字符,时间的方法_javascript技巧
-
JavaScript下的时间格式处理函数Date.prototype.format_javascript技巧
-
php用date格式化输出指定范围的时间_PHP教程
-
JAVA格式化时间日期的简单实例
-
Python简单格式化时间的方法【strftime函数】
-
Python简单格式化时间的方法【strftime函数】