js中怎么将时间戳转换为 yyyy-mm-dd格式

如题所述

需要准备的材料分别有:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html中的<script>标签,输入js代码:

var a = 1562133332000;

var date = new Date(a);

document.body.innerText = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDay();

3、浏览器运行index.html页面,此时发现时间戳被js成功转换为了yyyy-mm-dd格式。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-04-25
有三种常见方式:1、functiongetLocalTime(nS){returnnewDate(parseInt(nS)*1000).toLocaleString().replace(/:\d{1,2}$/,'');}alert(getLocalTime(1293072805));结果是2010年12月23日10:532、functiongetLocalTime(nS){returnnewDate(parseInt(nS)*1000).toLocaleString().substr(0,17)}alert(getLocalTime(1293072805));3、functiongetLocalTime(nS){returnnewDate(parseInt(nS)*1000).toLocaleString().replace(/年|月/g,"-").replace(/日/g,"");}alert(getLocalTime(1177824835));本回答被网友采纳