1.JS 格式化日期 2.JS中使用new Date(str)建立時間物件不相容firefox和ie的解決方式
阿新 • • 發佈:2018-12-20
將 2018-11-08 17:51:51 轉化為 2018年11月08日 或者其他格式..
//時間格式化 function time_format (time, format){ console.log(time); var t = new Date(time); var tf = function(i){return (i < 10 ? '0' : '') + i}; return format.replace(/yyyy|MM|dd|HH|mm|ss/g, function(a){ switch(a){ case 'yyyy': return tf(t.getFullYear()); break; case 'MM': return tf(t.getMonth() + 1); break; case 'mm': return tf(t.getMinutes()); break; case 'dd': return tf(t.getDate()); break; case 'HH': return tf(t.getHours()); break; case 'ss': return tf(t.getSeconds()); break; } }) }
使new Date(str)方法相容IE、firefox:
var date = new Date(strDate.replace("-", "/").replace("-", "/"));
綜合呼叫:
(1.其中paramTime是需要格式化的引數)
(2.其中yyyy年MM月dd日可以根據需要自定義)
time_format(new Date(paramTime.replace("-", "/").replace("-", "/")),'yyyy年MM月dd日')