1.任意格式的时间转换为毫秒数
var dateToTime = function(date){
//var time = (new Date(date)).getTime();
var time = (new Date(Date.parse(date.replace(/-/g,"/")))).getTime();//兼容IE
return time;
}
2.毫秒数转换为时间,格式为format(例如‘yyyy/MM/dd’)
var timeToDate = 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;
}
})
}