js时间戳与时间的相互转化

    //yyyy-mm-dd H:i转化为时间戳
    var datetime_to_unix = function(date){
        var tmp_datetime = date.replace(/:/g,'-');
        tmp_datetime = tmp_datetime.replace(/ /g,'-');
        var arr = tmp_datetime.split("-");
        var now = new Date(Date.UTC(arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4]));
        return parseInt(now.getTime()/1000);
    }
    //时间戳转化为当前时间
    var unix_to_datetime = function(x,unix) {
        var date = null;
        if(unix){
            date = new Date(unix);
        }else {
            date = new Date;
        }
        Y = date.getFullYear() + '-';
        M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1);
        D = date.getDate() < 10 ? '0' + date.getDate() + ' ' : date.getDate() + ' ';
        h = date.getHours() < 10 ? '0' + date.getHours()  + ':' : date.getHours() + ':';
        m = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
        s = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds();
        if(x === 1){return (Y+M+'-'+D+h+m);}//2017-09-29 21:30
        if(x === 4){return (Y+M+'-'+D+h+"00");}//2017-09-29 21:00
        if(x === 2){return (Y+M+'-'+D+"00"+":"+"00");}//2019-09-29 00:00
        if(x === 3){return (Y+M+'-'+D);}//2019-09-29
        if(x === 5){return (Y+M+'-'+"01");}//2019-09-01
        if(x === 6){return (Y+M);}//2019-09
        if(x === 7){return (Y+M+'-'+D+h+m+':'+s);}//精确到秒
        if(x === 0){//取上一个月
            var Y2 = date.getFullYear(); //获取当前日期的年份
            var M2 = parseInt(date.getMonth());
            if (M2 === 1) {//如果是1月份,则取上一年的12月份
                Y2 = parseInt(Y2) - 1;
                month2 = 12;
            }
            return (Y2+'-'+M2);
        }
    };


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答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月1日00: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月1日00: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月1日00: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、付费专栏及课程。

余额充值