js変換タイムスタンプ2017-01-01 24:00:00

1770 ワード

function theTime(tS) {
    //tS      ,         ,       parseInt(tS) * 1000
    var timeStr = new Date(parseInt(tS) * 1000).toLocaleString(); // 2017/7/28   1:36:36
    var timeArr = timeStr.split(" "); // timeArr[0] '2017/7/28'   timeArr[1] '  1:36:36'
    var t_arr_left = timeArr[0].split("/");
    var t_flag =true;
    for(var i = 1; i <= 2; i++) {
        if(parseInt(t_arr_left[i], 10) < 10) {
            t_arr_left[i] = "0" + t_arr_left[i];
        }
    }
    var new_time_left = t_arr_left.join("-");
    var t_arr_right = timeArr[1].split(":");
    if(t_arr_right[0].indexOf("  ") !== -1) {
        if(parseInt(t_arr_right[0].replace(/  /g, ""), 10) < 10) {
            t_arr_right[0] = "0" + t_arr_right[0].replace(/  /g, "");
        } else {
            if(parseInt(t_arr_right[0].replace(/  /g, ""), 10) == 12){ //     ,  12    12 
                t_arr_right[0] = '00';
            }else{
                t_arr_right[0] = t_arr_right[0].replace(/  /g, "");
            }
        }
        t_flag = false;
    }

    if(t_arr_right[0].indexOf("  ") !== -1) {
        if(parseInt(t_arr_right[0].replace(/  /g, ""), 10) == 12){ //     ,  12    12 
            t_arr_right[0] = (parseInt(t_arr_right[0].replace(/  /g, ""), 10)).toString();
        }else{
            t_arr_right[0] = (12 + parseInt(t_arr_right[0].replace(/  /g, ""), 10)).toString();
        }
        t_flag = false;
    }

    if(t_flag){
        return timeStr;
    }

    var new_time_right = t_arr_right.join(":");
    return new_time_left + " " + new_time_right;  //  2017-07-28 13:36:36


}