Javascript Date getWeekNumber()

Description

Javascript Date getWeekNumber()


Date.prototype.getWeekNumber = function(){
  var d = new Date(+this);
  d.setHours(0,0,0);//from w w w.  j a  va2s.  c  o m
  d.setDate(d.getDate()+4-(d.getDay()||7));
  return Math.ceil((((d-new Date(d.getFullYear(),0,1))/8.64e7)+1)/7);
};

Javascript Date getWeekNumber()

// 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  av  a2  s.c  o 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);
}

Javascript Date getWeekNumber()

Date.prototype.getWeekNumber = function () {
    var d = new Date(+this);
    d.setHours(0, 0, 0, 0);//from   ww  w.j a  v  a  2s .  c  o  m
    d.setDate(d.getDate() + 4 - (d.getDay() || 7));
    return Math.ceil((((d - new Date(d.getFullYear(), 0, 1)) / 8.64e7) + 1) / 7);
};

Date.prototype.addDays = function (days) {
    var dat = new Date(this.valueOf());
    dat.setDate(dat.getDate() + days);
    return dat;
};

function getDateOfISOWeek(w, y) {
    var simple = new Date(y, 0, 1 + (w - 1) * 7);
    var dow = simple.getDay();
    var ISOweekStart = simple;
    if (dow <= 4)
        ISOweekStart.setDate(simple.getDate() - simple.getDay() + 1);
    else
        ISOweekStart.setDate(simple.getDate() + 8 - simple.getDay());
    return ISOweekStart;
}

var getWeekNumber = function () {
    return weeknumber = new Date().getWeekNumber();
};

module.exports = {
    getWeekNumber
};



PreviousNext

Related