当前位置: 代码迷 >> JavaScript >> js 日期转换解决思路
  详细解决方案

js 日期转换解决思路

热度:155   发布时间:2012-02-20 21:18:23.0
js 日期转换
如何把   Wed,   25   Apr   2007   13:40:43   GMT   日期转换为   2007-4-25   13:40:43


------解决方案--------------------
<script>
function zero(num, s) {
var tmp = " " + num;
for (;tmp.length <s; tmp = "0 " + tmp);
return tmp;
}

function formatDate(d, fmt) {
var yyyy = d.getUTCFullYear();
var mm = zero(d.getUTCMonth() + 1, 2);
var dd = zero(d.getUTCDate(), 2);
var hh = zero(d.getUTCHours(), 2);
var mi = zero(d.getUTCMinutes(), 2);
var ss = zero(d.getUTCSeconds(), 2);

return fmt.replace(/yyyy/ig, yyyy).replace(/mm/ig, mm).replace(/dd/ig, dd).replace(/hh/ig, hh).replace(/mi/ig, mi).replace(/ss/ig, ss);
}

var d = new Date( "Wed, 25 Apr 2007 13:40:43 GMT ");

alert(formatDate(d, "yyyy-mm-dd hh:mi:ss "));
</script>
  相关解决方案