/** 格式化日期
* year 年份
* month 月份 从0开始计数。例如1月month为0,12月为11
* day 日
**/newDate(year, month, day)//例如:newDate(2020,10,1)//2020-11-01//当day为0时,结果为上个月的最后一天newDate(2020,10,0)//2020-10-30 //当 month > 11 时,year会增加newDate(2020,12,10)//2021-01-10newDate(2020,24,10)//2022-01-10......
3、常用事件整理
1. 日期加减天数
/** 日期加减天数
* date 日期
* days 天数
**/functionaddDate(date, days){var d =newDate(date);
d.setDate(d.getDate()+ days);var m = d.getMonth()+1;return d.getFullYear()+'-'+ m +'-'+ d.getDate();}
2. 格式化日期
/**格式化日期
* v 日期
* format 格式
**/functionformatDate(v, format){if(!v)return"";
format = format ||"yyyy-MM-dd";var d = v;if(typeof v ==='string'){if(v.indexOf("/Date(")>-1)
d =newDate(parseInt(v.replace("/Date(","").replace(")/",""),10));else
d =newDate(Date.parse(v.replace(/-/g,"/").replace("T"," ").replace(/\+/," +").split(".")[0]));//.split(".")[0] 用来处理出现毫秒的情况,截取掉.xxx,否则会出错}var o ={"M+": d.getMonth()+1,//month"d+": d.getDate(),//day"h+": d.getHours(),//hour"m+": d.getMinutes(),//minute"s+": d.getSeconds(),//second"q+": Math.floor((d.getMonth()+3)/3),//quarter"S": d.getMilliseconds()//millisecond};if(/(y+)/.test(format)){
format = format.replace(RegExp.$1,(d.getFullYear()+"").substr(4- RegExp.$1.length));}for(var k in o){if(newRegExp("("+ k +")").test(format)){
format = format.replace(RegExp.$1, RegExp.$1.length ==1? o[k]:("00"+ o[k]).substr((""+ o[k]).length));}}return format;};