首页 > 代码库 > 字符串时间转为时间戳,时间戳转为字符串时间

字符串时间转为时间戳,时间戳转为字符串时间

// 获取某个时间格式的时间戳
var stringTime = "2014-07-10 10:21:12";
var timestamp2 = Date.parse(new Date(stringTime));
timestamp2 = timestamp2 / 1000;

//格式化时间戳
function ChangeDateFormat(val) {
    if (val != null) {
        var date = new Date(parseInt(val.replace("/Date(", "").replace(")/", ""), 10));
        //月份为0-11,所以+1,月份小于10时补个0
        var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
        var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
        var currentHour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
        var currentMinute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
        return date.getFullYear() + "-" + month + "-" + currentDate + " " + currentHour + ":" + currentMinute;
    }

    return "";
}

 

字符串时间转为时间戳,时间戳转为字符串时间