原生js实现 日期转化为时间戳

//	日期转换为时间戳 2018-11-24 00:00:00->1543022952
function transfertime(string) {
    var f = string.split(' ', 2);//过滤空格
    if(f[0].search("/") != -1){//判断是否包含-
        var d = (f[0] ? f[0] : '').split('/', 3);//过滤-
    }else {
        var d = (f[0] ? f[0] : '').split('-', 3);//过滤-
    }

    var t = (f[1] ? f[1] : '').split(':', 3);//过滤:
    return (new Date(
        parseInt(d[0], 10) || null,
        (parseInt(d[1], 10) || 1) - 1,
        parseInt(d[2], 10) || null,
        parseInt(t[0], 10) || null,
        parseInt(t[1], 10) || null,
        parseInt(t[2], 10) || null
    )).getTime() / 1000;
}

猜你喜欢

转载自blog.csdn.net/u012967454/article/details/84869372