本文共 973 字,大约阅读时间需要 3 分钟。
var format = function(time, format){ 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; } })}alert(format(new Date().getTime(), 'yyyy-MM-dd HH:mm:ss')//new Date().getTime() 获取时间毫秒数
//IOS new Date() 中的时间不能用(2021-06-30 17:30) 这种格式.否则会报错(不能用 '-')//解决方法:使用正则转换一下var str = '2021-06-30 17:30'new Date(str.replace(/-/g,'/'));
转载地址:http://lgkgn.baihongyu.com/