web-dev-qa-db-fra.com

javascript calculer la date du numéro de la semaine

Comment puis-je calculer la date en javascript sachant le numéro de la semaine et l'année? Pour la semaine numéro 20 et l’année 2013, obtenir le 16/05/2013 Je tente donc:

Date.prototype.dayofYear = function() {        
    var d = new Date(this.getFullYear(), 0, 0);        
    return Math.floor((enter code here this - d) / 8.64e + 7); 
}
20
user2369009
function getDateOfWeek(w, y) {
    var d = (1 + (w - 1) * 7); // 1st of January + 7 days for each week

    return new Date(y, 0, d);
}

Ceci utilise la définition de simple week , ce qui signifie que la 20e semaine de 2013 est le 14 mai.

Pour calculer la date du début d'une semaine donnée ISO8601 (qui sera toujours un lundi)

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;
}

Résultat: la 20ème semaine de 2013 est le 13 mai, ce qui peut être confirmé ici .

57
Elle Trudgett

Semaines ISO

function weekDateToDate (year, week, day) {
  const firstDayOfYear = new Date(year, 0, 1)
  const days = 2 + day + (week - 1) * 7 - firstDayOfYear.getDay()
  return new Date(year, 0, days)
}
6
NoName

Désolé si c'est un peu bavard, mais cette solution renverra également le dimanche après avoir calculé le nombre de semaines. Il combine les réponses que j'ai vues de plusieurs endroits:

function getSundayFromWeekNum(weekNum, year) {
    var sunday = new Date(year, 0, (1 + (weekNum - 1) * 7));
    while (sunday.getDay() !== 0) {
        sunday.setDate(sunday.getDate() - 1);
    }
    return sunday;
}
5
nairys
function getDateOfWeek(weekNumber,year){
    //Create a date object starting january first of chosen year, plus the number of days in a week multiplied by the week number to get the right date.
    return new Date(year, 0, 1+((weekNumber-1)*7));
}
var myDate = getDateOfWeek(20,2013);
2
Salketer

Obtenir la plage de dates en fonction du numéro de semaine

Date.prototype.getWeek = function () {
    var target  = new Date(this.valueOf());
    var dayNr   = (this.getDay() + 6) % 7;
    target.setDate(target.getDate() - dayNr + 3);
    var firstThursday = target.valueOf();
    target.setMonth(0, 1);
    if (target.getDay() != 4) {
        target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
    }
    return 1 + Math.ceil((firstThursday - target) / 604800000);
}

function getDateRangeOfWeek(weekNo){
    var d1 = new Date();
    numOfdaysPastSinceLastMonday = eval(d1.getDay()- 1);
    d1.setDate(d1.getDate() - numOfdaysPastSinceLastMonday);
    var weekNoToday = d1.getWeek();
    var weeksInTheFuture = eval( weekNo - weekNoToday );
    d1.setDate(d1.getDate() + eval( 7 * weeksInTheFuture ));
    var rangeIsFrom = eval(d1.getMonth()+1) +"/" + d1.getDate() + "/" + d1.getFullYear();
    d1.setDate(d1.getDate() + 6);
    var rangeIsTo = eval(d1.getMonth()+1) +"/" + d1.getDate() + "/" + d1.getFullYear() ;
    return rangeIsFrom + " to "+rangeIsTo;
};

getDateRangeOfWeek(10)
"3/6/2017 to 3/12/2017"

getDateRangeOfWeek(30)
"7/24/2017 to 7/30/2017"
2
Aathi

Date de la semaine ISO (par exemple 2017-W32-3) à la date normale:

const getZeroBasedIsoWeekDay = date => (date.getDay() + 6) % 7
const getIsoWeekDay = date => getZeroBasedIsoWeekDay(date) + 1

function weekDateToDate(year, week, weekDay) {
  const zeroBasedWeek = week - 1
  const zeroBasedWeekDay = weekDay - 1
  let days = (zeroBasedWeek * 7) + zeroBasedWeekDay

  // Dates start at 2017-01-01 and not 2017-01-00
  days += 1

  const firstDayOfYear = new Date(year, 0, 1)
  const firstIsoWeekDay = getIsoWeekDay(firstDayOfYear)
  const zeroBasedFirstIsoWeekDay = getZeroBasedIsoWeekDay(firstDayOfYear)

  // If year begins with W52 or W53
  if (firstIsoWeekDay > 4) days += 8 - firstIsoWeekDay
  // Else begins with W01
  else days -= zeroBasedFirstIsoWeekDay

  return new Date(year, 0, days)
}

const expectedAndActual = [
  [new Date('2007-01-01'), weekDateToDate(2007,  1, 1)],
  [new Date('2015-11-24'), weekDateToDate(2015, 48, 2)],
  [new Date('2015-12-31'), weekDateToDate(2015, 53, 4)],
  [new Date('2016-01-03'), weekDateToDate(2015, 53, 7)],
  [new Date('2017-01-01'), weekDateToDate(2016, 52, 7)],
  [new Date('2017-01-02'), weekDateToDate(2017,  1, 1)],
  [new Date('2017-05-07'), weekDateToDate(2017, 18, 7)],
  [new Date('2018-12-31'), weekDateToDate(2019,  1, 1)],
]

expectedAndActual
  .forEach(value => {
    const expected = value[0].toISOString()
    const actual = value[1].toISOString()
    const isEqual = actual === expected
    console.assert(isEqual, '%s !== %s', actual, expected)
    console.log(actual, '===', expected)
  })

0
adius

Ceci est pour obtenir le premier lundi de la semaine de l'année en cours (ISO 8601):

function getFirstMondayOfWeek(weekNo) {
    var firstMonday = new Date(new Date().getFullYear(), 0, 4, 0, 0, 0, 0);

    while (firstMonday.getDay() != 1) {
        firstMonday.setDate(firstMonday.getDate() - 1);
    }
    if (1 <= weekNo && weekNo <= 52)
        return firstMonday.setDate(firstMonday.getDate() + 7 * (weekNo - 1));

    firstMonday.setDate(firstMonday.getDate() + 7 * (weekNo - 1));
    if (weekNo = 53 && firstMonday.getDate() >= 22 && firstMonday.getDate() <= 28)
        return firstMonday;
    return null;
}
0
Up Gxy