js 标准时间转化成毫秒时间戳
时间: 2023-02-08 16:19:51 浏览: 38
JS标准时间是指从1970年1月1日 00:00:00 UTC开始的时间,可以使用Date.parse()函数将其转换为毫秒时间戳。例如:
```
let timestamp = Date.parse("2020-01-01T00:00:00");
console.log(timestamp); // 1577836800000
```
注意,标准时间的格式必须符合 ISO 8601 格式,即 "YYYY-MM-DDTHH:mm:ss"。
相关问题
JS将时间转化为时间戳
在 JavaScript 中,可以使用 `Date.parse()` 方法或者 `Date.getTime()` 方法将时间转换为时间戳。下面是两种方法的具体实现:
1. 使用 `Date.parse()` 方法将时间转换为时间戳:
```js
const dateStr = '2021-10-20 14:30:00';
const timestamp = Date.parse(dateStr);
console.log(timestamp); // 输出时间戳,单位为毫秒
```
2. 使用 `Date.getTime()` 方法将时间转换为时间戳:
```js
const date = new Date('2021-10-20 14:30:00');
const timestamp = date.getTime();
console.log(timestamp); // 输出时间戳,单位为毫秒
```
上述代码中,第一种方法中我们将时间字符串 `2021-10-20 14:30:00` 转换为时间戳,其中 `Date.parse()` 方法返回的是从 1970 年 1 月 1 日 00:00:00 UTC(世界标准时间)到当前时间的毫秒数。
第二种方法中,我们先创建一个 `Date` 对象,然后调用 `getTime()` 方法得到时间戳。其中,`Date` 对象的构造函数可以接受一个时间字符串作为参数。
js将日期转化成时间戳
可以使用JavaScript中的`Date`对象来将日期转换为时间戳。`Date`对象的`getTime()`方法可以返回一个日期对象的时间戳,其单位为毫秒。下面是一个示例代码:
```javascript
var date = new Date('2021-08-01');
var timestamp = date.getTime();
console.log(timestamp); // 输出 1627785600000
```
在上面的示例中,我们先创建了一个`Date`对象,然后使用`getTime()`方法获取该日期对象的时间戳,并将其打印到控制台上。注意,时间戳的单位是毫秒,因此输出的结果是一个比较大的整数。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)