Get week of year in JavaScript like in PHP

后端 未结 19 1306
南方客
南方客 2020-11-22 02:38

How do I get the current weeknumber of the year, like PHP\'s date(\'W\')?

It should be the ISO-8601 week number of year, weeks starting

相关标签:
19条回答
  • 2020-11-22 02:50

    Accordily http://javascript.about.com/library/blweekyear.htm

    Date.prototype.getWeek = function() {
        var onejan = new Date(this.getFullYear(),0,1);
        var millisecsInDay = 86400000;
        return Math.ceil((((this - onejan) /millisecsInDay) + onejan.getDay()+1)/7);
    };
    
    0 讨论(0)
  • 2020-11-22 02:50

    Get the weeknumber of any given Date

    function week(year,month,day) {
        function serial(days) { return 86400000*days; }
        function dateserial(year,month,day) { return (new Date(year,month-1,day).valueOf()); }
        function weekday(date) { return (new Date(date)).getDay()+1; }
        function yearserial(date) { return (new Date(date)).getFullYear(); }
        var date = year instanceof Date ? year.valueOf() : typeof year === "string" ? new Date(year).valueOf() : dateserial(year,month,day), 
            date2 = dateserial(yearserial(date - serial(weekday(date-serial(1))) + serial(4)),1,3);
        return ~~((date - date2 + serial(weekday(date2) + 5))/ serial(7));
    }
    

    Example

    console.log(
        week(2016, 06, 11),//23
        week(2015, 9, 26),//39
        week(2016, 1, 1),//53
        week(2016, 1, 4),//1
        week(new Date(2016, 0, 4)),//1
        week("11 january 2016")//2
    );
    
    0 讨论(0)
  • 2020-11-22 02:50
    now = new Date();
    today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
    firstOfYear = new Date(now.getFullYear(), 0, 1);
    numOfWeek = Math.ceil((((today - firstOfYear) / 86400000)-1)/7);
    
    0 讨论(0)
  • 2020-11-22 02:53

    I tried a lot to get the shortest code to get the weeknumber ISO-conform.

    Date.prototype.getWeek=function(){
        var date=new Date(this);
        date.setHours(0,0,0,0);
        return Math.round(((date.setDate(this.getDate()+2-(this.getDay()||7))-date.setMonth(0,4))/8.64e7+3+(date.getDay()||7))/7)+"/"+date.getFullYear();}
    

    The variable date is necessary to avoid to alter the original this. I used the return values of setDate() and setMonth() to dispense with getTime() to save code length and I used an expontial number for milliseconds of a day instead of a multiplication of single elements or a number with five zeros. this is Date or Number of milliseconds, return value is String e.g. "49/2017".

    0 讨论(0)
  • 2020-11-22 02:56

    You should be able to get what you want here: http://www.merlyn.demon.co.uk/js-date6.htm#YWD.

    A better link on the same site is: Working with weeks.

    Edit

    Here is some code based on the links provided and that posted eariler by Dommer. It has been lightly tested against results at http://www.merlyn.demon.co.uk/js-date6.htm#YWD. Please test thoroughly, no guarantee provided.

    Edit 2017

    There was an issue with dates during the period that daylight saving was observed and years where 1 Jan was Friday. Fixed by using all UTC methods. The following returns identical results to Moment.js.

    /* For a given date, get the ISO week number
     *
     * Based on information at:
     *
     *    http://www.merlyn.demon.co.uk/weekcalc.htm#WNR
     *
     * Algorithm is to find nearest thursday, it's year
     * is the year of the week number. Then get weeks
     * between that date and the first day of that year.
     *
     * Note that dates in one year can be weeks of previous
     * or next year, overlap is up to 3 days.
     *
     * e.g. 2014/12/29 is Monday in week  1 of 2015
     *      2012/1/1   is Sunday in week 52 of 2011
     */
    function getWeekNumber(d) {
        // Copy date so don't modify original
        d = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate()));
        // Set to nearest Thursday: current date + 4 - current day number
        // Make Sunday's day number 7
        d.setUTCDate(d.getUTCDate() + 4 - (d.getUTCDay()||7));
        // Get first day of year
        var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
        // Calculate full weeks to nearest Thursday
        var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7);
        // Return array of year and week number
        return [d.getUTCFullYear(), weekNo];
    }
    
    var result = getWeekNumber(new Date());
    document.write('It\'s currently week ' + result[1] + ' of ' + result[0]);

    Hours are zeroed when creating the "UTC" date.

    Minimized, prototype version (returns only week-number):

    Date.prototype.getWeekNumber = function(){
      var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
      var dayNum = d.getUTCDay() || 7;
      d.setUTCDate(d.getUTCDate() + 4 - dayNum);
      var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
      return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
    };
    
    document.write('The current ISO week number is ' + new Date().getWeekNumber());

    Test section

    In this section, you can enter any date in YYYY-MM-DD format and check that this code gives the same week number as Moment.js ISO week number (tested over 50 years from 2000 to 2050).

    Date.prototype.getWeekNumber = function(){
      var d = new Date(Date.UTC(this.getFullYear(), this.getMonth(), this.getDate()));
      var dayNum = d.getUTCDay() || 7;
      d.setUTCDate(d.getUTCDate() + 4 - dayNum);
      var yearStart = new Date(Date.UTC(d.getUTCFullYear(),0,1));
      return Math.ceil((((d - yearStart) / 86400000) + 1)/7)
    };
    
    function checkWeek() {
      var s = document.getElementById('dString').value;
      var m = moment(s, 'YYYY-MM-DD');
      document.getElementById('momentWeek').value = m.format('W');
      document.getElementById('answerWeek').value = m.toDate().getWeekNumber();      
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
    
    Enter date  YYYY-MM-DD: <input id="dString" value="2021-02-22">
    <button onclick="checkWeek(this)">Check week number</button><br>
    Moment: <input id="momentWeek" readonly><br>
    Answer: <input id="answerWeek" readonly>

    0 讨论(0)
  • 2020-11-22 02:56

    Jacob Wright's Date.format() library implements date formatting in the style of PHP's date() function and supports the ISO-8601 week number:

    new Date().format('W');
    

    It may be a bit overkill for just a week number, but it does support PHP style formatting and is quite handy if you'll be doing a lot of this.

    0 讨论(0)
提交回复
热议问题