Small micro-channel program, the time stamp into date format

 

 Usually backstage pass over all the time stamp, but can not show a time stamp showing the front desk. We need transformed.

Function Description:

Small micro-channel program, the time stamp into date format, support for custom,

Copy to the project utils / utils.js and notice under declared js;

= {module.exports 
    FormatTime: FormatTime, // transfer date stamp 
    formatTimeTwo: formatTimeTwo // turn date stamp
}

utils / utils.js wording

Copy the code

FormatTime function (DATE) { 
    var year = the Date.getFullYear () 
    var month The date.getMonth = () +. 1 
    var Day = date.getDate () 

    var hour = date.getHours () 
    var = date.getMinutes minute () 
    var SECOND date.getSeconds = () 


    return [year, month The, Day] .map (formatNumber) .join ( '/') + '' + [hour, minute, SECOND] .map (formatNumber) .join ( ':') 
} 

formatNumber function (n-) { 
    n-n.toString = () 
    return n-[. 1] n-:? '0' + n- 
} 

/ ** 
 * when the date stamp is converted to minutes and seconds 
 * number: incoming timestamp 
 * format: return format, support for custom, but the parameters must be kept in consistent with the formateArr 
* / 
function formatTimeTwo (Number the, format) {

    var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
    var returnArr = [];

    var date = new Date(number * 1000);
    returnArr.push(date.getFullYear());
    returnArr.push(formatNumber(date.getMonth() + 1));
    returnArr.push(formatNumber(date.getDate()));

    returnArr.push(formatNumber(date.getHours()));
    returnArr.push(formatNumber(date.getMinutes()));
    returnArr.push(formatNumber(date.getSeconds()));

    for (var i in returnArr) {
        format = format.replace(formateArr[i], returnArr[i]);
    }
    return format;
}

module.exports = {
    formatTime: formatTime,
    formatTimeTwo:formatTimeTwo  
}

Copy the code

Js call in the following, first of all have introduced until js

was h = require ( '../../ utils / util.js');

 

Copy the code

SJC = 1,488,481,383 var; 
    the console.log (time.formatTime (SJC, 'the Y / M / D H: m: S')); 
    the console.log (time.formatTime (SJC, 'H: m')); 



obtained results are as follows: 

2017/03/03 03:03:03 
03:03

Copy the code

Guess you like

Origin blog.csdn.net/qq_36935391/article/details/90906489