1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| function getCurrentDate(){ // 获取当前日期 var date = new Date();
// 获取当前月份,js月份从 0-11 var nowMonth = date.getMonth() + 1;
// 获取当前是几号 var strDate = date.getDate();
// 添加分隔符“-” var seperator = "-";
// 对月份进行处理,1-9月在前面添加一个“0” if (nowMonth >= 1 && nowMonth <= 9) { nowMonth = "0" + nowMonth; }
// 对月份进行处理,1-9号在前面添加一个“0” if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; }
// 最后拼接字符串,得到一个格式为(yyyy-MM-dd)的日期 var nowDate = date.getFullYear() + seperator + nowMonth + seperator + strDate; return nowDate }
|