/** * The timestamp is converted to date time *@param Timestamp: pass in the timestamp. It must be a 10-bit string or a 13-bit string *@param Format: Indicates the date format. The value must be Y M D h M s. The format can be customized, for example, 'y-m-d h: M :s'
var timestampToTime = function (timestamp ,format) {
    var format_Arr = ['Y'.'M'.'D'.'h'.'m'.'s'];

    timestamp = timestamp.toString().length === 13 ? Number(timestamp) : timestamp*1000;
    var data = new Date(timestamp);
    var Y = zerofill(data.getFullYear()),
        M = zerofill(data.getMonth()+1),
        D = zerofill(data.getDate()),
        h = zerofill(data.getHours()),
        m = zerofill(data.getMinutes()),
        s = zerofill(data.getMilliseconds());

    var time_Arr = [Y, M, D, h, m, s];

    time_Arr.forEach(function (value, index) {
        format = format.replace(format_Arr[index], value);
    });

    return format;

    function zerofill (time) {
        var tiem_str = time.toString();
        return tiem_str[1]? tiem_str :'0'+ tiem_str; }};Copy the code

References: www.jianshu.com/p/5d380b68c…