JS計算指定日期是距今的第幾周,星期幾

來源:互聯網
上載者:User

標籤:style   blog   color   io   使用   ar   sp   div   2014   


無意中在百度知道上發現這樣一個問題,就抽時間見寫了一個函數。

首先我們需要明確,既然是指定日期距今的第幾周,那麼就要知道指定的日期是什麼,而且是不能確定的,會根據使用者不同而得到不同的日期,所以我們需要將這個日期設定為參數,由使用者決定。

下面我們一起來看一下這段代碼:

 1 (function(){ 2     /* 3      * 說明: 4      * 一周的起始計算方式不同國家有所不同,很多其他國家將周日作為一周的開始 5      * 本代碼使用中國習慣,將周一作為每周的開始 6      * 特此說明 7      */ 8  9     function TodayInfo(start) {10         var WEEKLEN = 7, // 一周7天為常量11             WEEKDAYS = ["日", "一", "二", "三", "四", "五", "六"],12             weekInfo = {"week": null, "day": null}, // 初始化返回資訊,預設第null周,星期null13             oneDay = 24 * 60 * 60 * 1000, // 一天的毫秒時間長度14             weekLeave, // 開學當天所在周剩餘天數15             weekStart, // 開學當天start是星期幾16             today, // 今天17             dateDiff, // 今天與開學當天日期差18             sDate; //開學之日,日期對象19         var rDateStr = /\d{4}[\/-]\d{1,2}[\/-]\d{1,2}/g; // 簡單的日期格式校正:2013/12/1920         if (!rDateStr.test(start)) {21             alert("請使用合法的開學日期!!!");22             return weekInfo;23         }24         sDate = new Date(start.replace("-", "/"));25         weekStart = sDate.getDay();26         weekStart = weekStart === 0 ? 7 : weekStart; // JS中周日的索引為0,這裡轉換為7,方便計算27         28         weekLeave = WEEKLEN - weekStart;29         today = new Date();30         weekInfo.day = WEEKDAYS[today.getDay()];31         today = new Date(today.getFullYear() + "/" + (today.getMonth() + 1) + "/" + today.getDate());32         dateDiff = today - sDate;33         if (dateDiff < 0) {34             alert("別開玩笑了,你還沒開學呢!!!");35             return weekInfo;36         }37         dateDiff = parseInt(dateDiff / oneDay);38         weekInfo.week = Math.ceil((dateDiff - weekLeave) / WEEKLEN) + 1;39         return weekInfo;40     }41     // 測試結果42     var td = TodayInfo("2013/12/16");43     console.log("今天是自2013/12/16日,開學以來的第 " + td.week + " 周,今天星期" + td.day);44     td = TodayInfo("2013/11/11");45     console.log("今天是自2013/11/11日,開學以來的第 " + td.week + " 周,今天星期" + td.day);46     td = TodayInfo("2013/09/01");47     console.log("今天是自2013/09/01日,開學以來的第 " + td.week + " 周,今天星期" + td.day);48     td = TodayInfo("2013/12/29");49     console.log("今天是自2013/12/29日,開學以來的第 " + td.week + " 周,今天星期" + td.day);50 })();

 

這裡在跟大家介紹一些關於Date對象的提示:

  1. 擷取前一月的總天數
    1 var a = new Date(), total = 0;2 a.setDate(0);3 total = a.getDate(); // 30
  2. 計算兩個日期之間的時間差:
    1 var total = new Date(‘2014/10/13‘) - new Date();

     

JS計算指定日期是距今的第幾周,星期幾

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.