javascript convert timestamp to time format (processed compatible format)

Hello everyone, I'm Xiao C, let's talk about the method of converting timestamps to time. This is something I usually use when doing AJAX. The time values ​​I get back from PHP in the background are timestamps. , the number of digits of the timestamp is not the same. Now the PHP that I cooperate with gives 10 digits, and the front-end code obtains 13 digits. In fact, it is just more milliseconds. Each language The number of bits obtained by each method is not necessarily the same, but it doesn't matter, it can be handled. Not much to say, go directly to the code:

The first method: convert the timestamp to the actual time (no format compatibility, not recommended)

// nS is a ten-digit timestamp    
function getLocalTime(str) {
    return  new Date(parseInt(str) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ' );
} 
// Usage:
getLocalTime(str)

 

The second method: (It is also the most recommended use: compatibility processing is done, and the format will not be different under each browser!)

 // The parameter str is the timestamp, which can be passed in 10 digits or 13 digits.
 // The value of the parameter bool can be passed true or false or not passed. If the second needs to be displayed, pass true, and if it does not need to be displayed, pass false or not pass
function getMyDate(str, bool){ 
     if (str > 9999999999) { // Judging here: the timestamp is a few digits 
        var c_Date = new Date(parseInt(str));
    } else {
        var c_Date = new Date(parseInt(str) * 1000);
    }
    var c_Year = c_Date.getFullYear(), 
    c_Month = c_Date.getMonth()+1, 
    c_Day = c_Date.getDate(),
    c_Hour = c_Date.getHours(), 
    c_Min = c_Date.getMinutes(), 
    c_Sen = c_Date.getSeconds();
     if (bool) { // Determine whether seconds need to be displayed 
        var c_Time = c_Year +'-'+ getzf(c_Month) +'-'+ getzf(c_Day) +' '+ getzf(c_Hour) +':'+ getzf(c_Min) +':'+getzf(c_Sen); // last splice time 
    } else {
         var c_Time = c_Year +'-'+ getzf(c_Month) +'-'+ getzf(c_Day) + ' '+ getzf(c_Hour) +':'+ getzf(c_Min); // last stitching time 
    }
     return c_Time;
};
// Add 0 in front of the number if the 0 operation is less than 10, which should be well understood 
function getzf(c_num){ if (parseInt(c_num) < 10 ){ c_num = '0' + c_ num; } return c_num; } // Usage: // Need to display seconds: getMyDate(1523927510, true) // Do not need to display seconds: ① getMyDate(1523927510, false) ② getMyDate(1523927510) // If only time is needed: getMyDate(1523927510, true).split (" ")[1]; // If only the date is needed: getMyDate(1523927510, true).split(" ")[0];

These are my two methods. Of course, I recommend the second one. Well, I will share these today. If there are any deficiencies, please give me more advice. You are welcome to express your opinions or suggestions in the comment area.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324586294&siteId=291194637