1、时间戳转日期格式:
1 function timestampToTime(timestamp) { 2 var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000 3 Y = date.getFullYear() + '-'; 4 M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'; 5 D = date.getDate() + ' '; 6 h = date.getHours() + ':'; 7 m = date.getMinutes() + ':'; 8 s = date.getSeconds(); 9 return Y + M + D + h + m + s; 10 }
2、日期转时间戳:
1 var date = new Date('2014-04-23 18:55:49:123'); 2 // 有三种方式获取 3 var time1 = date.getTime(); 4 var time2 = date.valueOf(); 5 var time3 = Date.parse(date); 6 console.log(time1);//1398250549123 7 console.log(time2);//1398250549123 8 console.log(time3);//1398250549000