js日期转换为yyyy-mm-dd
方法参数说明:
date:要转换的日期
cut:日期显示符("-“或”/")
isTime:是否返回时间
function formatDate(date,cut,isTime) {
var date = new Date(date);
var YY = date.getFullYear() + cut;
var MM =(date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + cut;
var DD = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
var hh =(date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
var mm =(date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +":";
var ss = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
return YY + MM + DD + (isTime == true ? " " + hh + mm + ss : "");
}
例子:
var date=formatDate(new Date(),"-",false);
快去试试吧!