<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>js将时间戳转换成正常时间格式</title> </head> <body> </body> <script type="text/javascript"> // 方法一 function timestampTime(timestamp) { var date = new Date(timestamp * 1000); //时间戳为10位需*1000,时间戳为13位的话不需乘1000 var Y = date.getFullYear() + '-'; var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'; var D = date.getDate() + ' '; var h = date.getHours() + ':'; var m = date.getMinutes() + ':'; var s = date.getSeconds(); return Y + M + D + h + m + s; } var time = timestampTime(1177824835); console.log(time); //2007-04-29 13:33:55 // 方法二 var time = new Date(parseInt(1177824835) * 1000).toLocaleString().replace(/:\d{1,2}$/,' '); console.log(time); //2007/4/29 下午1:33 </script> </html>