js时间戳与时间日期间相互转换

本文详细介绍如何在JavaScript中将日期转换为时间戳,以及如何将时间戳转换回日期格式。包括使用newDate()获取当前日期并转换为时间戳,指定日期的转换,以及将时间戳格式化为特定日期格式的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  今天在工作中要将获取到的时间转换为时间戳,一时间竟不知道怎么用,于是不得不去查询资料,这里特地做个笔记。

  1、将日期转换为时间戳。

  要将日期转换为时间戳,首先得先获取到日期,这里可以直接指定日期,或者是使用当前日期。要获取当前日期,我们可以使用new Date()来获取。直接上代码。

// (1)、将当前日期转换为时间戳。
    var now = new Date();
    console.log(now.getTime())  // 将当前日期转换为时间戳,getTime()方法可返回距1970年1月1日之间的毫秒数。也可以使用  +now ,该效果等同于now.getTime()

// (2)、将指定日期转换为时间戳。
    var t = "2017-12-08 20:5:30";  // 月、日、时、分、秒如果不满两位数可不带0.
    var T = new Date(t);  // 将指定日期转换为标准日期格式。Fri Dec 08 2017 20:05:30 GMT+0800 (中国标准时间)
    console.log(T.getTime())  // 将转换后的标准日期转换为时间戳。  

  2、将时间戳转换为日期。

var t = 787986456465;  // 当参数为数字的时候,那么这个参数就是时间戳,被视为毫秒,创建一个距离1970年1月一日指定毫秒的时间日期对象。
console.log(new Date(t)) // Wed Dec 21 1994 13:07:36 GMT+0800 (中国标准时间)

var t2 = "2017-5-8 12:50:30";
console.log(new Date(t2)) // Mon May 08 2017 12:50:30 GMT+0800 (中国标准时间)

var t3 = "2017-10-1";
console.log(new Date(t3)) // Sun Oct 01 2017 00:00:00 GMT+0800 (中国标准时间) 不设定时分秒,则默认转换为00:00:00

 将时间戳转换为指定格式日期的方法封装:

// 格式化日期,如月、日、时、分、秒保证为2位数
function formatNumber (n) {
    n = n.toString()
    return n[1] ? n : '0' + n;
}
// 参数number为毫秒时间戳,format为需要转换成的日期格式
function formatTime (number, format) {
    let time = new Date(number)
    let newArr = []
    let formatArr = ['Y', 'M', 'D', 'h', 'm', 's']
    newArr.push(time.getFullYear())
    newArr.push(formatNumber(time.getMonth() + 1))
    newArr.push(formatNumber(time.getDate()))

    newArr.push(formatNumber(time.getHours()))
    newArr.push(formatNumber(time.getMinutes()))
    newArr.push(formatNumber(time.getSeconds()))

    for (let i in newArr) {
        format = format.replace(formatArr[i], newArr[i])
    }
    return format;
}

 如需要调用上述方法,使用formatTime(1545903266795, 'Y年M月D日 h:m:s')或者formatTime(1545903266795, 'Y-M-D h:m:s')即可

### 回答1: 在 JavaScript 中,可以使用如下两个方法将时间戳转换时间: 1. `new Date(timestamp)`: 将时间戳作为参数传入 `Date` 构造函数,就可以得到一个时间对象。例如: ``` const timestamp = 1609459200000; const date = new Date(timestamp); console.log(date); // Mon Dec 21 2020 00:00:00 GMT+0800 (中国标准时间) ``` 2. `Date.parse(dateString)`: 将时间字符串作为参数传入 `Date.parse` 方法,就可以得到时间戳。例如: ``` const dateString = "Mon Dec 21 2020 00:00:00 GMT+0800"; const timestamp = Date.parse(dateString); console.log(timestamp); // 1609459200000 ``` 此相反,可以使用如下方法将时间转换时间戳: 1. `Date.getTime()`: 将时间对象作为参数传入 `getTime` 方法,就可以得到时间戳。例如: ``` const date = new Date(); const timestamp = date.getTime(); console.log(timestamp); // 1609527540379 ``` 2. `Date.parse(dateString)`: 将时间字符串作为参数传入 `Date.parse` 方法,就可以得到时间戳。例如: ``` const dateString = "Mon Dec 21 2020 00:00:00 GMT+0800"; const timestamp = Date.parse(dateString); console.log(timestamp); // 1609459200000 ``` ### 回答2: 在JavaScript中,我们可以使用`Date`对象来进行时间戳时间之间的转化。 1. 时间戳时间时间戳是指从1970年1月100:00:00 UTC(协调世界时)起经过的毫秒数。我们可以使用`new Date()`函数将时间戳转换为可读的时间格式。 ```javascript const timestamp = 1617185631067; // 时间戳 const date = new Date(timestamp); // 时间戳时间 console.log(date); // 输出:Thu Mar 18 2021 19:40:31 GMT+0800 (中国标准时间) ``` 2. 时间时间戳: 我们可以使用`getTime()`方法来将时间转换时间戳。该方法返回距离1970年1月100:00:00 UTC(协调世界时)的毫秒数。 ```javascript const date = new Date(); // 当前时间 const timestamp = date.getTime(); // 时间时间戳 console.log(timestamp); // 输出当前时间时间戳 ``` 通过上述的代码示例,我们可以轻松地在JavaScript中进行时间戳时间之间的转化。 ### 回答3: 在JavaScript中,可以用当前时间生成一个时间戳,也可以将一个时间戳转化为相应的时间格式。 将当前时间转化为时间戳可以使用`getTime()`方法。例如,可以使用以下代码获取当前时间时间戳: ``` let timestamp = new Date().getTime(); ``` 这将返回当前时间时间戳,单位为毫秒。 要将一个时间戳转化为时间格式,可以使用`toLocaleString()`方法。例如,可以使用以下代码将一个时间戳转化为本地时间字符串: ``` let timestamp = 1609459200000; // 假设时间戳为2021年1月100:00:00 let date = new Date(timestamp); let timeString = date.toLocaleString(); console.log(timeString); ``` 这将输出:2021/1/1 上午12:00:00(根据本地时间格式可以有所不同)。 如果希望时间格式更加可读性强,可以使用`getFullYear()`、`getMonth()`、`getDate()`、`getHours()`、`getMinutes()`和`getSeconds()`等方法来获取年、月、、小时、分钟和秒等信息,然后再自行组合成需要的时间格式。 总之,在JavaScript中,可以轻松地相互转化时间戳时间格式,便于在应用中进行时间的处理和展示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值