js print date time method

js prints the current time, first of all js has the time output. 
Use Date() to get the time. But the international standard time format is as follows: 
document.write(Date()) 
Mon Jul 24 2017 21:29:35 GMT+0800 (China Standard Time) 
This time format is often not what we want, we can extract it according to the standard time , compose the format we want. 
var dateDigitToString = function (num) { 
        return num < 10 ? '0' + num : num; 
    };            
Note: The function of this function is two digits, if the ten digit does not have a number, fill in "0". 
    var currentDate = new Date(), 
        year = dateDigitToString(currentDate.getFullYear()),// Get the current year 

        month = dateDigitToString(currentDate.getMonth() + 1), // Get the current month
       Note: Because the month is based on 0--11, you need to add 1; 
        date = dateDigitToString(currentDate.getDate()), // Get Current date
        hour = dateDigitToString(currentDate.getHours()), // Get the current time
        minute = dateDigitToString(currentDate.getMinutes()),//  Get the current minute
        second = dateDigitToString(currentDate.getSeconds()),// Get the current second 

        formattedDateString = year + 'year' + month + 'month' + date + 'day' + hour + ':' + minute + ':' + second;//Get the format we want; 

document.write(formattedDateString);

July 24, 2017 21:40:42

Guess you like

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