js时间戳和php时间戳

js时间转换

1、JavaScript 的时间对象转换为时间戳

1.1、时间对象转时间戳的 5 种写法

在 JavaScript 中,将时间对象转换为时间戳的方法有 5 种,示例如下:

// 定义一个时间对象 dt,然后依次演示各种将 dt 转换为时间戳的写法
var dt = new Date("2019-07-04 23:59:59.999");

// 写法一,精确到毫秒,得到 13 位时间戳 1562255999999
console.log(dt.getTime());

// 写法二,精确到毫秒,得到 13 位时间戳 1562255999999
console.log(dt.valueOf());

// 写法三,精确到毫秒,得到 13 位时间戳 1562255999999
console.log(Number(dt));

// 写法四,精确到毫秒,得到 13 位时间戳 1562255999999
console.log(+dt);

// 写法五,精确到秒,得到 13 位时间戳 1562255999000,后三位固定为 000
console.log(Date.parse(dt));

1.2、获得当前时间的时间戳

在 JavaScript 中,当前时间可用new Date()表示,再结合上文中时间对象转时间戳的写法,那么就能获得当前时间的时间戳了,示例如下:

new Date().getTime();   // 方法一
new Date().valueOf();   // 方法二
Date.parse(new Date()); // 方法三
Number(new Date());     // 方法四
+new Date();            // 方法五

.3、获得 10 位数的时间戳

在 JavaScript 中,通过时间对象转换得到的时间戳都是 13 位的,但有时候我们也需要精确到秒的 10 位时间戳,比如微信支付用的就是 10 位的时间戳。要在 JavaScript 获得 10 位的时间戳,大致思路有两个,要么截取前 10 位,要么除以 1000。示例如下:

// 将 13 位时间戳除以 1000 然后再取整,得到 10 位时间戳数字
parseInt(+new Date()/1000);

// 将 13 位时间戳转换为字符串截取前 10 位,得到 10 位时间戳字符串
(+new Date()).toString().substring(0,10); // 截取第 0~9 位
(+new Date()).toString().substr(0,10);    // 从第 0 位开始截取 10 位

4、时间的加减

  

 当使用js日期对象Date进行时间加减时,这个处理相对比较简单,直接调用Date对象的方法进行操作。只是Date对象最后需要进行格式化(格式化可以参考这篇文章“js 常用日期字符串和日期转换 ”),这样显示比较友好。

    示例代码如下

var curTime = new Date();
// curTime: Tue Oct 13 2020 16:44:47 GMT+0800 (中国标准时间)
console.log('curTime:', curTime);

// 1. 加上1个小时
var addHour = curTime.setHours(curTime.getHours() + 1);
// addHour: 1602582287529
console.log('addHour:', addHour);
// Tue Oct 13 2020 17:44:47 GMT+0800 (中国标准时间)
console.log(new Date(addHour));

// 2. 在当前时间curTime变量上加上10分钟
var addMinute = new Date(curTime.setMinutes(curTime.getMinutes() + 10));
// addMinute: Tue Oct 13 2020 17:54:47 GMT+0800 (中国标准时间)
console.log('addMinute:', addMinute);

// 3. 在当前时间curTime变量上加上1分40秒(100秒)
var addSeconds = new Date(curTime.setSeconds(curTime.getSeconds() + 100));
// addSeconds: Tue Oct 13 2020 17:56:27 GMT+0800 (中国标准时间)
console.log('addSeconds:', addSeconds);

5、 使用字符串日期的处理,首先需要将字符串转换为js的Date对象。后续的处理,和第一步相同。

    代码如下:

var rawdate = stringToDate('2020-10-13 12:00:01');
// 日期加上11秒
var addSecond = 11;
var resDate = new Date(rawdate.setSeconds(rawdate.getSeconds() + addSecond));
var res = format(resDate, "yyyy-MM-dd hh:mm:ss");
console.log(res);

// js字符串转日期Date
// 字符串格式:2020-10-13 12:00:01
function stringToDate(strDate) {
    var tempStrs = strDate.split(" ");

    var dateStrs = tempStrs[0].split("-");
    var year = parseInt(dateStrs[0], 10);
    var month = parseInt(dateStrs[1], 10) - 1;
    var day = parseInt(dateStrs[2], 10);

    var timeStrs = tempStrs[1].split(":");
    var hour = parseInt(timeStrs [0], 10);
    var minute = parseInt(timeStrs[1], 10);
    var second = parseInt(timeStrs[2], 10);

    var date = new Date(year, month, day, hour, minute, second);
    return date;
}

// js日期Date格式化为字符串
// 字符串格式:2020-10-13 12:00:01
function format(date, fmt) {
    var o = {
        "M+": date.getMonth() + 1, //月份
        "d+": date.getDate(), //
        "h+": date.getHours(), //小时
        "m+": date.getMinutes(), //
        "s+": date.getSeconds(), //
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ?
                (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
    }
    return fmt;
}

 

posted on 2023-10-17 16:58  陈可  阅读(221)  评论(0)    收藏  举报

导航