Nodejs Week Get getWeekNumber()

Here you can find the source of getWeekNumber()

Method Source Code

// week number starts at mon 00 am and ends at sun 00 pm
Date.prototype.getWeekNumber = function(){
   var d = new Date(+this);
   d.setHours(0,0,0);/*w w w.j  a  va2s . co  m*/
   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")
      emit(docdate.getFullYear() + "" + docdate.getWeekNumber(), doc);
}

Related

  1. getWeekDayName()
    Date.prototype.getWeekDayName = function(){
      return this.weekDayName()[this.getDay()];
    };
    Date.prototype.weekDayName = function(){
      return ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
    };
    sec = 1000;
    min = 60*sec;
    hour = 60*min;
    ...
    
  2. 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'],
    ...
    
  3. 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'],
    ...
    
  4. 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);
    };
    
  5. 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);
    };
    
  6. getWeekNumber()
    Date.prototype.getWeekNumber = function() {
        var target = new Date(this.valueOf());
        var dauNumber = (this.getDay() + 6) % 7;
        target.setDate(target.getDate() - dauNumber + 3);
        var jan4 = new Date(target.getFullYear(), 0, 4);
        var dayDiff = (target - jan4) / 86400000;
        if (new Date(target.getFullYear(), 0, 1).getDay() < 5) {
            return 1 + Math.ceil(dayDiff / 7);
        else {
            return Math.ceil(dayDiff / 7);
    
  7. 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);
    };
    
  8. 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);
    };
    
  9. 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);
    };