时间戳
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 </body> 9 <script> 10 //时间戳转换成日期,日期转换成时间戳 11 var d = new Date(1293072805); 12 console.log(d.getFullYear());//1970 13 console.log(d.getMonth() + 1);//1 14 console.log(d.getDate());//15 15 16 d.getTime();//日期转换成时间戳 17 Date.parse();//日期转换成时间戳 18 19 20 //时间戳转换成日期 21 22 //方法1 23 function getLocalTime(nS) { 24 return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' '); 25 } 26 console.log(getLocalTime(1293072805)); 27 28 //方法2 29 function getLocalTime(nS) { 30 return new Date(parseInt(nS) * 1000).toLocaleString().substr(0,17) 31 } 32 console.log(getLocalTime(1293072805)); 33 34 //方法3 35 function formatDate(now) { 36 var year=now.getFullYear(); 37 var month=now.getMonth()+1; 38 var odate=now.getDate(); 39 var hour=now.getHours(); 40 var minute=now.getMinutes(); 41 var second=now.getSeconds(); 42 return year+"-"+month+"-"+odate+" "+hour+":"+minute+":"+second; 43 } 44 45 var d=new Date(1230999938); 46 console.log(formatDate(d)); 47 48 49 50 </script> 51 </html>