1.
Date.prototype.format = function(format) { var o = { "M+" : this.getMonth()+1, //month "d+" : this.getDate(), //day "h+" : this.getHours(), //hour "m+" : this.getMinutes(), //minute "s+" : this.getSeconds(), //second "q+" : Math.floor((this.getMonth()+3)/3), //quarter "S" : this.getMilliseconds() //millisecond } if(/(y+)/.test(format)) format=format.replace(RegExp.$1,(this.getFullYear()+"").substr(4 - RegExp.$1.length)); for(var k in o) if(new RegExp("("+ k +")").test(format)) format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length)); return format; }
After that, you can use the format method to determine the date and time format you need, such as:
var mydate=new Date().format('yyyy year mm month dd day hh:mm:ss');
2.
dateDigitToString = function (num) { return num < 10 ? '0' + num : num; var currentDate = new Date(), year = dateDigitToString(currentDate.getFullYear()), month = dateDigitToString(currentDate.getMonth() + 1), date = dateDigitToString(currentDate.getDate()), hour = dateDigitToString(currentDate.getHours()), minute = dateDigitToString(currentDate.getMinutes()), second = dateDigitToString(currentDate.getSeconds()), formattedDateString = year + '年' + month + '月' + date + '日 ' + hour + ':' + minute + ':' + second;
This method can only define one time and date format, which is relatively less widely used than the first one.