0

Is there anyway to get the last calender week of the year in javascript?

This year for example "52".

Patrick Grebe
  • 111
  • 2
  • 14

2 Answers2

0

This will return the last week number of year.

Date.prototype.getWeek = function() {
    var onejan = new Date(this.getFullYear(),0,1);
    var lastDate = new Date("12/31/" + this.getFullYear());
    
    var dayOfYear = ((lastDate - onejan +1)/86400000);
    return Math.ceil(dayOfYear/7)
};


jQuery(function(){  
    var lastDate = new Date();
    var weekno = lastDate.getWeek();
    $("#weekNo").html(weekno);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="weekNo"></div>
Hamza Haider
  • 730
  • 6
  • 20
0

you can do something like this :

function getStartingWeek(year, week) {
    let tmp = new Date(year, 0, 1);
    tmp.setDate(tmp.getDate() - tmp.getDay() + week * 7);
    return tmp;
}
getStartingWeek(2018, 52) // give the ending day date of the 52th week of the year 2018
Djory Krache
  • 347
  • 4
  • 9