1. The current time is converted to "yyyy-MM-dd HH:MM:SS"
function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var seperator2 = ":"; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds(); return currentdate; }
2. Convert any date to "yyyy-MM-dd HH:MM:SS" and pass the date variable as a parameter.
3. Convert any date to "yyyy-MM-dd", just change it slightly.
// Get the current date and time format "yyyy-MM-dd" function getFormatDate(date) { //var date = new Date(); var seperator1 = "-"; var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate; return currentdate; }
4. Convert any date to " HH:MM:SS ", just change it slightly, omit it.
5. Convert the specified format to "yyyy-MM-dd" format, the code is as follows:
var oldTime = (new Date("2012/12/25 20:11:11")).getTime();
var curTime = new Date(oldTime).format("yyyy-MM-dd");
console.log(curTime);
6. Convert "timestamp" to "year month day" format.
For example the following code:
var da = 1402233166999;
da = new Date(da);
var year = da.getFullYear();
var month = da.getMonth()+1;
var date = da.getDate();
console.log([year,month,date].join('-'));
Reprinted source: https://www.cnblogs.com/tugenhua0707/p/3776808.html