12345
| // 获取当前时间戳(以s为单位)var timestamp = Date.parse(new Date());timestamp = timestamp / 1000;//当前时间戳为:1403149534console.log("当前时间戳为:" + timestamp);
|
123456
| // 获取某个时间格式的时间戳var stringTime = "2014-07-10 10:21:12";var timestamp2 = Date.parse(new Date(stringTime));timestamp2 = timestamp2 / 1000;//2014-07-10 10:21:12的时间戳为:1404958872 console.log(stringTime + "的时间戳为:" + timestamp2);
|
时间戳转日期格式
123456
| <script> function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' '); } alert(getLocalTime(1293072805)); </script>
|
结果是 2010-12-23 10:53
123456
| <script> function getLocalTime(nS) { return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " "); } alert(getLocalTime(1177824835)); </script>
|
结果是 2010年12月23日 10:53
12
| console.log(Date.parse(new Date('2017-07-03 23:59:59')) / 1000); //转为时间戳console.log(new Date(parseInt(1499097599) * 1000).toLocaleString()); //转为日期
|
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理