日期處理方法
阿新 • • 發佈:2017-06-20
col r+ code param aso 日期處理 new 上月 obj
//格式化日期:yyyy-MM-dd function formatDate(date) { 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 getMonthDays(paraYear,paraMonth){ var monthStartDate = new Date(paraYear, paraMonth, 1); var monthEndDate = new Date(paraYear, paraMonth + 1, 1); var days = (monthEndDate - monthStartDate)/(1000 * 60 * 60 * 24); returndays; } //獲得某周的開始日期 function getWeekStartDate(paraYear,paraMonth,paraDay,paraDayOfWeek) { var weekStartDate = new Date(paraYear, paraMonth, paraDay + 1 - paraDayOfWeek); return formatDate(weekStartDate); } //獲得某周的結束日期 function getWeekEndDate(paraYear,paraMonth,paraDay,paraDayOfWeek) {var weekEndDate = new Date(paraYear, paraMonth, paraDay + (7 - paraDayOfWeek)); return formatDate(weekEndDate); } //獲得某月的開始日期 function getMonthStartDate(paraYear,paraMonth){ var monthStartDate = new Date(paraYear, paraMonth, 1); return formatDate(monthStartDate); } //獲得某月的結束日期 function getMonthEndDate(paraYear,paraMonth){ var monthEndDate = new Date(paraYear,paraMonth, getMonthDays(paraYear,paraMonth)); return formatDate(monthEndDate); } //獲得上月開始時間 function getLastMonthStartDate(paraYear, lastMonth){ var lastMonthStartDate = new Date(paraYear, lastMonth, 1); return formatDate(lastMonthStartDate); } //獲得上月結束時間 function getLastMonthEndDate(paraYear, lastMonth, lastMonth){ var lastMonthEndDate = new Date(paraYear, lastMonth, getMonthDays(lastMonth)); return formatDate(lastMonthEndDate); } //獲得某季度的開始日期 function getQuarterStartDate(paraYear,paraSeason){ switch (paraSeason){ case ‘1‘ : return paraYear+"-01-01"; case ‘2‘ : return paraYear+"-04-01"; case ‘3‘ : return paraYear+"-07-01"; case ‘4‘ : return paraYear+"-10-01"; } } //獲得某季度的結束日期 function getQuarterEndDate(paraYear,paraSeason){ switch (paraSeason){ case ‘1‘ : return paraYear+"-03-31"; case ‘2‘ : return paraYear+"-06-30"; case ‘3‘ : return paraYear+"-09-30"; case ‘4‘ : return paraYear+"-12-31"; } } //獲取某年某周的開始日期 function getBeginDateOfWeek(paraYear, weekIndex){ var firstDay = GetFirstWeekBegDay(paraYear); //7*24*3600000 是一星期的時間毫秒數,(JS中的日期精確到毫秒) var time=(weekIndex-1)*7*24*3600000; var beginDay = firstDay; //為日期對象 date 重新設置成時間 time beginDay.setTime(firstDay.valueOf()+time); return formatDate(beginDay); } //獲取某年某周的結束日期 function getEndDateOfWeek(paraYear, weekIndex){ var firstDay = GetFirstWeekBegDay(paraYear); //7*24*3600000 是一星期的時間毫秒數,(JS中的日期精確到毫秒) var time=(weekIndex-1)*7*24*3600000; var weekTime = 6*24*3600000; var endDay = firstDay; //為日期對象 date 重新設置成時間 time endDay.setTime(firstDay.valueOf()+weekTime+time); return formatDate(endDay); } //獲取日期為某年的第幾周 function GetWeekIndex(dateobj) { var firstDay = GetFirstWeekBegDay(dateobj.getFullYear()); if (dateobj < firstDay) { firstDay = GetFirstWeekBegDay(dateobj.getFullYear() - 1); } d = Math.floor((dateobj.valueOf() - firstDay.valueOf()) / 86400000); return Math.floor(d / 7) + 1; } //獲取某年的第一天 function GetFirstWeekBegDay(year) { var tempdate = new Date(year, 0, 1); var temp = tempdate.getDay(); if (temp == 1){ return tempdate; } temp = temp == 0 7 : temp; tempdate = tempdate.setDate(tempdate.getDate() + (8 - temp)); return new Date(tempdate); }
日期處理方法