1.yyyy-MM-dd(年月日)
/**
* yyyy-MM--dd
* */
getNowFormatDate: function () {
var date = new Date();
var seperator1 = "-";
var year = date.getFullYear();
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 = year + seperator1 + month + seperator1 + strDate;
return currentdate;
},
2.yyyy-MM-dd HH:mm:ss(年月日 时分秒)
/**
* yyyy-MM-dd HH:mm:ss
*/
getNowFormatDate2: function () {
var date = new Date();
var seperator1 = "-";
var seperator2 = ":";
//外国的月份都是从0开始的,所以+1
var month = date.getMonth() + 1;
var strDate = date.getDate();
//1-9月用0补位
if(month >=1 && month <=9){
month = "0" + month;
}
//1-9日用0补位
if(strDate >=0 && strDate <= 9){
strDate = "0" + strDate;
}
//获取当前时间 yyyy-MM-dd HH:mm:ss
var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " +date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds();
return currentdate;
}
3.效果