| 赋某日期为日期类型 | new Date(myYear,myMonth,myDate);//当myDate为0时,返回当前月份的上月末日期 |
|
当前日期 now | new Date() |
|
设置日期分钟为0 | now.setMinutes(0) |
|
设置当前日期秒为1 | now.setSeconds(1) |
|
当前日期为本周的第几天,星期三返回3 nowDayOfWeek | now.getDay() |
|
当前日 nowDay | now.getDate() |
|
当前月 nowMonth | now.getMonth();//0为一月,1为2月,... |
|
当前年 nowYear |
nowYear = now.getYear(); nowYear += nowYear<2000?1900:0; |
| 格式化日期:yyyy-MM-dd | function formatDate(date,fmt) { var myyear = date.getFullYear(); var mymonth = date.getMonth()+1; var myweekday = date.getDate(); if(mymonth < 10){ mymonth = "0" + mymonth; } if(myweekday < 10){ myweekday = "0" + myweekday; } return (myyear+"-"+mymonth + "-" + myweekday); } |
| 格式化日期:任意格式 | function dateFtt(fmt,date) { var o = { "M+" : date.getMonth()+1, //月份 "d+" : date.getDate(), //日 "h+" : date.getHours(), //小时 "m+" : date.getMinutes(), //分 "s+" : date.getSeconds(), //秒 "q+" : Math.floor((date.getMonth()+3)/3), //季度 "S" : date.getMilliseconds() //毫秒 }; if(/(y+)/.test(fmt)) fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length)); for(var k in o) if(new RegExp("("+ k +")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length))); return fmt; } |
| 计算月份的天数 | function getMonthDays(nowYear,myMonth){ //此处计算的是一个月的天数 var monthStartDate = new Date(nowYear, myMonth, 1); var monthEndDate = new Date(nowYear, myMonth + 1, 1); var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24); return days; } |
|
本周的开始日期 weekStartDate | new Date(nowYear, nowMonth, nowDay - nowDayOfWeek); |
|
本周的结束日期 weekEndDate | new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek)); |
|
本月初 monthStartDate | new Date(nowYear, nowMonth, 1); |
| now.setDate(1); | |
|
本月末 monthEndDate | var = new Date(nowYear, nowMonth, getMonthDays(nowMonth)); |
|
本月所属季度 nowQuarter |
nowMonth ++; parseInt(nowMonth/3+(nowMonth%3==0?0:1)) |
|
nowMonth ++; Math.ceil(nowMonth/3) | |
|
上月初 lastMonthStart | new Date(nowYear, lastMonth, 1); |
|
lastMonthDate = new Date(); lastMonthDate.setDate(1); lastMonthDate.setMonth(lastMonthDate.getMonth()-1) | |
|
new Date(myYear,myMonth-1,1) | |
|
上月末 lastMonthEnd | new Date(nowYear, lastMonth, getMonthDays(lastMonth)); |
| new Date(myYear,myMonth,0); | |
|
本季度开始月份 quarterStartMonth | if(nowMonth<3){ return 0; }else if(2<nowMonth && nowMonth<6){ return 3; }else if(5<nowMonth && nowMonth<9){ return 6; }else{ //if(nowMonth>8){ return 9; } |
|
本季度的开始日期 quarterStartDate | new Date(nowYear, quarterStartMonth, 1); |
|
本季度的结束日期 quarterEndDate | var quarterEndMonth = quarterStartMonth + 2; var quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth)); |
| var quarterEndMonth = quarterStartMonth + 2; var quarterStartDate = new Date(nowYear, quarterEndMonth+1, 0); | |
| 取与今天偏差为o天的日期 | new Date(now.getTime()+o*24*60*60*1000) |
| 计算两个日期的相差天数 |
function dateDiff(startDate, endDate){ var start = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate()); var end = new Date(endDate.getFullYear(), endDate.getMonth(), endDate.getDate()); return parseInt(Math.abs(end - start ) / 1000 / 60 / 60 /24); }; |
本文详细介绍了JavaScript中日期的各种操作方法,包括获取当前日期、格式化日期、计算日期差、获取特定日期如本周开始和结束日期、上个月初和月末日期等。通过这些方法,读者可以更好地理解和掌握日期处理技巧。
1万+

被折叠的 条评论
为什么被折叠?



