閒聊:新年第一天上班,看着本身15年年末寫的代碼,真心以爲很爛,由於年末沒時間去寫,一想着作後臺管理須要獲取一週的開始和結束日期,就慌了,項目趕着測試呢,還有好多事情未作,就直接抄襲了網上的一段錯誤代碼,其實真的很簡單,今天從新花點時間封裝一下,以備後用,大神勿噴,謝謝!webpack
jQuery.dateCollections = (function(){ var nowDate = new Date(); var cloneNowDate = new Date(); var fullYear = nowDate.getFullYear(); var month = nowDate.getMonth() + 1; // getMonth 方法返回 0-11,表明1-12月 var date = nowDate.getDate(); var endOfMonth = new Date(fullYear, month, 0).getDate(); // 獲取本月最後一天 // 格式化日期 (2016-02-14) function getFullDate(targetDate) { var D, y, m, d; if (targetDate) { D = new Date(targetDate); y = D.getFullYear(); m = D.getMonth() + 1; d = D.getDate(); } else { y = fullYear; m = month; d = date; } m = m > 9 ? m : '0' + m; d = d > 9 ? d : '0' + d; return y + '-' + m + '-' + d; } // 一天的時間戳(毫秒爲單位) var timestampOfDay = 1000*60*60*24; // 今天,昨天 var fullToday = getFullDate(); var fullYesterday = getFullDate(nowDate - timestampOfDay); var nowDay = nowDate.getDay(); // getDay 方法返回0 表示星期天 nowDay = nowDay === 0 ? 7 : nowDay; // 本週一,本週末(星期天) // 注:在safari下(nowDate +- 0)不會轉換爲時間戳,這裏在nowDate前加上運算符+,手動轉換時間戳運算 var fullMonday = getFullDate( +nowDate - (nowDay-1)*timestampOfDay ); var fullSunday = getFullDate( +nowDate + (7-nowDay)*timestampOfDay ); // 月初,月末 var fullStartOfMonth = getFullDate( cloneNowDate.setDate(1) ); var fullEndOfMonth = getFullDate( cloneNowDate.setDate(endOfMonth) ); return { fullToday: fullToday, fullYesterday: fullYesterday, fullMonday: fullMonday, fullSunday: fullSunday, fullStartOfMonth: fullStartOfMonth, fullEndOfMonth: fullEndOfMonth }; }());
var nowDate = new Date(); var cloneNowDate = new Date(); var fullYear = nowDate.getFullYear(); var month = nowDate.getMonth() + 1; // getMonth 方法返回 0-11,表明1-12月 var date = nowDate.getDate(); var endOfMonth = new Date(fullYear, month, 0).getDate(); // 獲取本月最後一天 // 格式化日期 (2016-02-14) function getFullDate(targetDate) { var D, y, m, d; if (targetDate) { D = new Date(targetDate); y = D.getFullYear(); m = D.getMonth() + 1; d = D.getDate(); } else { y = fullYear; m = month; d = date; } m = m > 9 ? m : '0' + m; d = d > 9 ? d : '0' + d; return y + '-' + m + '-' + d; } // 一天的時間戳(毫秒爲單位) var timestampOfDay = 1000*60*60*24; // 今天,昨天 var fullToday = getFullDate(); var fullYesterday = getFullDate(nowDate - timestampOfDay); var nowDay = nowDate.getDay(); // getDay 方法返回0 表示星期天 nowDay = nowDay === 0 ? 7 : nowDay; // 本週一,本週末(星期天) // 注:在safari下(nowDate +- 0)不會轉換爲時間戳,這裏在nowDate前加上運算符+,手動轉換時間戳運算 var fullMonday = getFullDate( +nowDate - (nowDay-1)*timestampOfDay ); var fullSunday = getFullDate( +nowDate + (7-nowDay)*timestampOfDay ); // 月初,月末 var fullStartOfMonth = getFullDate( cloneNowDate.setDate(1) ); var fullEndOfMonth = getFullDate( cloneNowDate.setDate(endOfMonth) ); module.exports = { fullToday: fullToday, fullYesterday: fullYesterday, fullMonday: fullMonday, fullSunday: fullSunday, fullStartOfMonth: fullStartOfMonth, fullEndOfMonth: fullEndOfMonth };
總結:代碼中就兩難點,一是獲取月末的最後一天日期,二是求 一週的開始和結束日期時間,第一點是知不知道的問題,第二點是比較今日與週末(星期天),週一各差幾天,加減相差的時間戳獲取相應的日期,總之說難不難,說不難可能當你在百忙之中確實要花點時間,保持清醒的頭腦;web
轉載請註明出處:博客園(楊君華的博客)工具