//JavaScript Document var finalMonth =['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']; var finalWeek = ['Mon','Tues','Wed','Thur','Fri','Sat','Sun']; // date : Mon May 11 13:53:08 UTC+0800 2015 /** * param Date對象:Mon May 11 13:53:08 UTC+0800 2015 * return yyyy-MM-dd */ function getTodayFormatea(param){ var result = ""; if(param==null){ param = new Date(); } var dateStr = param.toString(); var dateArr = dateStr.split(" "); var dateMonth= getMonthNum(dateArr[1]); var dateDay = dateArr[2]; var dateYear = dateArr[5]; if(dateDay<10){ dateDay="0"+dateDay; } if(dateMonth<10){ dateMonth="0"+dateMonth; } result = ""+dateYear+"-"+dateMonth+"-"+dateDay; return result; } /** * param Date對象:Mon May 11 13:53:08 UTC+0800 2015 * return yyyy/MM/dd */ function getTodayFormate(param){ var result = ""; if(param==null){ param = new Date(); } var dateStr = param.toString(); var dateArr = dateStr.split(" "); var dateMonth= getMonthNum(dateArr[1]); var dateDay = dateArr[2]; var dateYear = dateArr[5]; if(dateDay<10){ dateDay="0"+dateDay; } if(dateMonth<10){ dateMonth="0"+dateMonth; } result = ""+dateYear+"/"+dateMonth+"/"+dateDay; return result; } /** * param Date Object:Mon May 11 13:53:08 UTC+0800 2015 * n 自param 起向後多少天 * return Date Object:Mon May 11 13:53:08 UTC+0800 2015 */ function getDateBeforeParamDate(param,n){ var result = new Date(); if(param==null){ param = result; } if(n==null){ n=0; } param = new Date(param); millSecond = param.valueOf(); millSecond = millSecond - n*24*3600*1000; result = new Date(millSecond); result = getTodayFormatea(result); return result; } /** * param Date對象:Mon May 11 13:53:08 UTC+0800 2015 * n 自param 向前多少天 * return Date Object:Mon May 11 13:53:08 UTC+0800 2015 */ function getDateNextParamDate(param,n){ var result = new Date(); if(param==null){ param = result; } if(n==null){ n=0; } param = new Date(param); millSecond = param.valueOf(); millSecond = millSecond + n*24*3600*1000; result = new Date(millSecond); result = getTodayFormatea(result); return result; } /** * 根據月份簡稱獲取獲取對應的數值 * */ function getMonthNum(param){ var result = 0; for(var i=1;i<13;i++){ if(param==finalMonth[i-1]){ result=i; } } return result; } /** * 根據星期簡稱獲取對應的數值 * */ function getWeekNum(param){ var result = 0; for(var i=1;i<8;i++){ if(param == finalWeek[i-1]){ result =i; } } return result; }