用JavaScript 获取各种日期-方法
//获取当前日期【type: 1-年 2-月 3-日 4-分秒 5-周】
function getCurrentDate(format) {
debugger
var now = new Date();
var year = now.getFullYear(); //得到年份
var month = now.getMonth(); //得到月份
var date = now.getDate(); //得到日期
var day = now.getDay(); //得到周几
var hour = now.getHours(); //得到小时
var minu = now.getMinutes(); //得到分钟
var sec = now.getSeconds(); //得到秒
month = month + 1;
if (month < 10) month = "0" + month;
if (date < 10) date = "0" + date;
if (hour < 10) hour = "0" + hour;
if (minu < 10) minu = "0" + minu;
if (sec < 10) sec = "0" + sec;
var time = "";
//精确到年
if (format == 1) {
time = year;
}
//精确到月
else if (format == 2) {
time = year + "-" + month;
}
//精确到日
else if (format == 3) {
time = year + "-" + month + "-" + date;
}
//精确到分
else if (format == 4) {
time = year + "-" + month + "-" + date + " " + hour + ":" + minu + ":" + sec;
}
//精确到周
else if (format == 5) {
// 本周⼀的⽇期
var begin = year + "-" + month + "-" + (date - day + 1);
// 本周⽇的⽇期
var end = year + "-" + month + "-" + (date - day + 7);
time = begin +" ~ "+ end
}
return time;
}