JS获取指定时间的时间戳的方法汇总(最新整理收藏版)

 更新时间:2024年01月30日 15:59:55   作者:XiaoSen125_  
在JavaScript中,可以使用Date.parse()或new Date()来获取指定时间的时间戳,本文给大家分享JS获取指定时间的时间戳的方法,感兴趣的朋友一起看看吧

JS获取指定时间的时间戳

在JavaScript中,可以使用Date.parse()new Date()来获取指定时间的时间戳。

方法一:使用Date.parse()

可以使用Date.parse()方法将指定时间转换为时间戳,具体步骤如下:

var timestamp = Date.parse("2023-02-25 10:30:00"); //将时间字符串转换为时间戳
console.log(timestamp); //输出时间戳

其中,需要注意的是,Date.parse()方法传入的时间字符串需要符合ISO 8601标准或是RFC 2822标准,否则可能会返回NaN

方法二:使用new Date()

可以使用new Date()方法创建一个指定时间的Date对象,然后调用getTime()方法获取时间戳,具体步骤如下:

var date = new Date("2023-02-25 10:30:00"); //创建指定时间的Date对象
var timestamp = date.getTime(); //获取时间戳
console.log(timestamp); //输出时间戳

需要注意的是,new Date()方法传入的时间字符串需要符合ISO 8601标准或是RFC 2822标准,否则可能会返回无效的Date对象。

补充:

js获取时间戳

一、Date.now()   精确到毫秒 13位

// 获取当前时间戳
const timestamp = Date.now()
console.log(timestamp)   //1685632984861

二、getTime()   精确到毫秒 13位

// 获取当前时间戳
const timestamp = new Date().getTime();
console.log(timestamp);
// 获取自定义时间戳 
// 1、只传日期不传时间(后面是该时间戳的时间)
const t1 = new Date('2023-06-01').getTime()  //[2023-06-01 08:00:00]
// 2、如果日期没有补0
const t2 = new Date('2023-6-01').getTime()   //[2023-06-01 00:00:00]
// 3、如果是传斜杠格式(补不补0是一样的)
const t3 = new Date('2023/06/01').getTime()  //[2023-06-01 00:00:00]
// 4、传了日期+时间就得到该时间的时间戳
const t4 = new Date('2023-06-01 12:00:00').getTime() // [2023-06-01 12:00:00]
const t5 = new Date('2023/6/1 12:00:00').getTime()   // [2023-06-01 12:00:00]

三、+new Date()   精确到毫秒 13位

// 获取当前时间戳
const timestamp = +new Date();
console.log(timestamp);
// 获取今天0点(比如今天是[2023-06-01],那么就获取到[2023-06-01 00:00:00]的时间戳)
const timestamp = new Date().setHours(0, 0, 0, 0)
// 获取今天24点(比如今天是[2023-06-01],那么就获取到[2023-06-02 00:00:00]的时间戳)
const timestamp = new Date().setHours(24, 0, 0, 0)
// 获取近七天时间戳
// 用今天0点的时间戳,减去6天的毫秒数即可   每天的毫秒数为24 * 60 * 60 * 1000
// 比如今天是[2023-06-07],那么就获取到[2023-06-01 00:00:00]的时间戳
const sevenDaysBefore = new Date().setHours(0, 0, 0, 0) - 6 * 24 * 60 * 60 * 1000

四、Date.parse(new Date())   精确到秒 13位

// 获取当前时间戳
const timestamp = Date.parse(new Date());
console.log(timestamp);
// 获取自定义时间戳 
const t1 = Date.parse("2023-06-01")   //[2023-06-01 08:00:00]
const t2 = Date.parse("2023-6-1")     //[2023-06-01 00:00:00]
const t3 = Date.parse("2023/6/1")     //[2023-06-01 00:00:00]
const t4 = Date.parse("2023-06-01 12:00:00") //[2023-06-01 12:00:00]
const t5 = Date.parse("2023/6/1 12:00:00")   //[2023-06-01 12:00:00]

五、valueOf()   精确到毫秒 13位

// 获取当前时间戳
const timestamp = (new Date()).valueOf();
console.log(timestamp);
// 获取自定义时间戳 
const t1 = new Date("2023-06-01").valueOf()  // [2023-06-01 08:00:00]
const t2 = new Date("2023-6-1").valueOf()    // [2023-06-01 00:00:00]
const t3 = new Date('2023/6/1').valueOf()    // [2023-06-01 00:00:00]
const t4 = new Date('2023-06-01 12:00:00').valueOf() // [2023-06-01 12:00:00]
const t5 = new Date('2023/6/1 12:00:00').valueOf()   // [2023-06-01 12:00:00]

六、Number(new Date())   精确到毫秒 13位

// 获取当前时间戳
const timestamp = Number(new Date());
console.log(timestamp);
// 获取自定义时间戳 
const t1 = Number(new Date("2023-06-01"));   //[2023-06-01 08:00:00]
const t2 = Number(new Date("2023-6-1"));     //[2023-06-01 00:00:00]
const t3 = Number(new Date("2023/6/1"));     //[2023-06-01 00:00:00]
const t4 = Number(new Date("2023-06-01 12:00:00"));   //[2023-06-01 12:00:00]
const t5 = Number(new Date("2023/6/1 12:00:00"));     //[2023-06-01 12:00:00]

到此这篇关于JS获取指定时间的时间戳的文章就介绍到这了,更多相关js获取指定时间时间戳内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论