//获得上个月在昨天这一天的日期 function getLastMonthYestdy(date){ var daysInMonth = new Array([0],[31],[28],[31],[30],[31],[30],[31],[31],[30],[31],[30],[31]); var strYear = date.getFullYear(); var strDay = date.getDate(); var strMonth = date.getMonth()+1; if(strYear%4 == 0 && strYear%100 != 0){ daysInMonth[2] = 29; } if(strMonth - 1 == 0) { strYear -= 1; strMonth = 12; } else { strMonth -= 1; } strDay = daysInMonth[strMonth] >= strDay ? strDay : daysInMonth[strMonth]; if(strMonth<10) { strMonth="0"+strMonth; } if(strDay<10) { strDay="0"+strDay; } datastr = strYear+"-"+strMonth+"-"+strDay; return datastr; } //计算天数差的函数,通用 function DateDiff(sDate, eDate){ //sDate1和sDate2是2006-12-18格式 var aDate, oDate1, oDate2, iDays; aDate = sDate.split("-"); oDate1 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]); //转换为12-18-2006格式 aDate = eDate.split("-"); oDate2 = new Date(aDate[1] + '-' + aDate[2] + '-' + aDate[0]); iDays = parseInt(Math.abs(oDate1 - oDate2) / 1000 / 60 / 60 /24) ; //把相差的毫秒数转换为天数 return iDays } //比较日期大小 function DateComparison(startTime, endTime) { var arr = startTime.split("-"); var starttime = new Date(arr[0], arr[1], arr[2]); var starttimes = starttime.getTime(); var arrs = endTime.split("-"); var lktime = new Date(arrs[0], arrs[1], arrs[2]); var lktimes = lktime.getTime(); if (starttimes >lktimes) { return false; } else return true; }
JS对日期的相关操作
最新推荐文章于 2024-01-25 14:00:18 发布