Nodejs Week Get getWeekNumber()

Here you can find the source of getWeekNumber()

Method Source Code

Date.prototype.getWeekNumber = function() {
    // Create a copy of this date object
    var target = new Date(this.valueOf());
    // ISO week date weeks start on monday, so correct the day number
    var dauNumber = (this.getDay() + 6) % 7;
    // Set the target to the thursday of this week so the
    // target date is in the right year
    target.setDate(target.getDate() - dauNumber + 3);

    // ISO 8601 states that week 1 is the week with january 4th in it
    var jan4 = new Date(target.getFullYear(), 0, 4);
    // Number of days between target date and january 4th
    var dayDiff = (target - jan4) / 86400000;

    if (new Date(target.getFullYear(), 0, 1).getDay() < 5) {
        // Calculate week number: Week 1 (january 4th) plus the
        // number of weeks between target date and january 4th
        return 1 + Math.ceil(dayDiff / 7);
    }/*from w w w.j  av a 2s .  c  o m*/
    else {
        // jan 4th is on the next week (so next week is week 1)
        return Math.ceil(dayDiff / 7);
    }
}

Related

  1. getWeekDayName(lang)
    Date.prototype.getWeekDayName = function(lang) {
        lang = lang && (lang in Date.locale) ? lang : 'de';
        return Date.locale[lang].day_names[this.getDay()];
    };
    Date.locale = {
        de: {
            month_names: ['Januar', 'Februar', 'M?', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
            month_names_short: ['Jan', 'Feb', 'M?, 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'],
            day_names: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
    ...
    
  2. getWeekDayNameShort(lang)
    Date.prototype.getWeekDayNameShort = function(lang) {
        lang = lang && (lang in Date.locale) ? lang : 'de';
        return Date.locale[lang].day_names_short[this.getDay()];
    };
    Date.locale = {
        de: {
            month_names: ['Januar', 'Februar', 'M?', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember'],
            month_names_short: ['Jan', 'Feb', 'M?, 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez'],
            day_names: ['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
    ...
    
  3. getWeekNumber()
    Date.prototype.getWeekNumber = function(){
        const date = new Date(+this);
        date.setHours(0,0,0);
        date.setDate(date.getDate()+4-(date.getDay()||7));
        return Math.ceil((((date-new Date(date.getFullYear(),0,1))/8.64e7)+1)/7);
    };
    
  4. getWeekNumber()
    Date.prototype.getWeekNumber = function(){
        var d = new Date(+this);
        d.setHours(0,0,0);
        d.setMilliseconds(0);
        d.setDate(d.getDate()+4-(d.getDay()||7));
        return Math.ceil((((d-new Date(d.getFullYear(),0,1))/8.64e7)+1)/7);
    };
    
  5. getWeekNumber()
    Date.prototype.getWeekNumber = function(){
      var d = new Date(+this);
      d.setHours(0,0,0);
      d.setDate(d.getDate()+4-(d.getDay()||7));
      return Math.ceil((((d-new Date(d.getFullYear(),0,1))/8.64e7)+1)/7);
    };
    function(doc) {
      var docdate = new Date(doc.created_at);
      if (doc.type === "entry")
    ...
    
  6. getWeekNumber()
    Date.prototype.getWeekNumber = function(){
        var d = new Date(+this);
        d.setHours(0,0,0);
        d.setDate(d.getDate()+4-(d.getDay()||7));
        return Math.ceil((((d-new Date(d.getFullYear(),0,1))/8.64e7)+1)/7);
    };
    
  7. getWeekNumber()
    Date.prototype.getWeekNumber = function () {
        var d = new Date(+this);
        d.setHours(0, 0, 0, 0);
        d.setDate(d.getDate() + 4 - (d.getDay() || 7));
        return Math.ceil((((d - new Date(d.getFullYear(), 0, 1)) / 8.64e7) + 1) / 7);
    };
    
  8. getWeekNumber()
    Date.prototype.getWeekNumber = function () {
      var d = new Date(this);
      d.setHours(0, 0, 0);
      d.setDate(d.getDate() + 4 - (d.getDay() || 7));
      return Math.ceil((((d - new Date(d.getFullYear(), 0, 1)) / 8.64e7) + 1) / 7);
    };
    
  9. getWeekOfMonth()
    Date.prototype.getWeekOfMonth = function () {
      var today = new Date(this.getFullYear(), this.getMonth(), this.getDate());
      var firstOfMonth = new Date(today.getFullYear(), today.getMonth(), 1);
      return Math.ceil((((today - firstOfMonth) / 86400000) + firstOfMonth.getDay() + 1) / 7);
    };