700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > javascript:时间戳转换为日期时间

javascript:时间戳转换为日期时间

时间:2020-01-08 11:38:17

相关推荐

javascript:时间戳转换为日期时间

首先,在script里面,定义一个方法,具体如下:

function formatDate(now) {var year = now.getFullYear(),month = now.getMonth() + 1,date = now.getDate(),hour = now.getHours(),minute = now.getMinutes(),second = now.getSeconds();return year + "-" + month + "-" + date;}

然后再处理接收过来的时间戳数据

var time = formatDate(new Date(data['time']*1000));

【注意事项】

1、在调用formatDate方法时,切记里面要有new Date。

2、另外,得到的时间,必须乘以1000。(具体为啥我也不知道,网上有不少说出现NaN-NaN-NaN的解决方法,感觉都不如这个好使。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。