首页 > 编程知识 正文

js获取时间戳,获取当前时间戳的函数

时间:2023-05-03 13:38:34 阅读:268461 作者:2216

JS获取当前时间戳的方法 & 中国标准时间改为formatTime格式 JS获取当前时间戳的方法

//方法一

var timestamp = (new Date()).getTime();console.log(timestamp); //1605671286580

//方法二

var timestamp2 = (new Date()).valueOf();console.log(timestamp2); //1605671332439

//方法三

var timestamp3 = Date.parse(new Date());console.log(timestamp3);//1605671286000

第一种和第二种是获取了当前毫秒的时间戳
最后一种获取的时间戳是把毫秒改成000显示

中国标准时间改为formatTime格式

获取标准时间

var time = new Date();time// Wed Nov 18 2020 11:46:30 GMT+0800 (中国标准时间)

1、toLocaleDateString (根据本地时间把Date 对象的日期部分转换为字符串):

var time = new Date();var formatTime = time.toLocaleDateString();//print"2020/11/18"

2、将时间指定为 年-月-日 格式,例:2017-1-1

var date = new Date('Thu May 12 2016 08:00:00 GMT+0800 (中国标准时间)'); formatTime=date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();//print formatTime2016-5-12

这里要注意月份的获取,表示月份的参数介于 0 到 11 之间,所以如果想获得当前真实月份,还需 +1。

实例

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。