2

I have searched the web and found the script to get the week number in year. However my counting is difference. The below image is the week number I want to get. When I tested using '1/5/2015', my code got week number is 2, but the week number should be 1 in my requirement. Would someone can help me. Thanks in advance.

enter image description here

I found the javascript at IamSilviu/Get week number

There is my code:

function myWeekNumber(thisDate) {
var dt = new Date(thisDate)
    var onejan=new Date(dt.getFullYear(), 0, 2);
    return Math.ceil((((dt - onejan) / 86400000) + onejan.getDay() + 1) / 7);    }
user819774
  • 1,456
  • 1
  • 19
  • 48
  • so determine if first week of year is full or not and adjust – epascarello Oct 05 '17 at 01:31
  • see answer in [this so question](https://stackoverflow.com/questions/9045868/javascript-date-getweek) - is that correct? – Jaromanda X Oct 05 '17 at 01:49
  • Please avoid ambiguous dates. To me, "1/5/2015" is 1 May 2015 but from the context of the question I expect you think it's 5 January 2015. Also, you need to define how you determine that it's in week 2. The ISO week number starts on the first week containing a Thursday in the year, so that makes it the second week. What algorithm do you want to use? – RobG Oct 05 '17 at 02:55
  • Seems like you need this lib https://github.com/datejs/Datejs – prabhatojha Oct 05 '17 at 06:15
  • `new Date(dt.getFullYear(), 0, 2)` creates a date for 2 Jan, not 1. You need to start from the first day of the first week (which if often in the previous year), not 1 Jan. – RobG Oct 05 '17 at 12:04
  • @epascarello, the counting is first week of year is full. – user819774 Oct 05 '17 at 21:24

2 Answers2

0

The algorithm you're trying to implement seems to be that:

  1. Weeks start on Sunday
  2. The first week of the year is the one that has any days in the year, e.g. 1 Jan 2016 was a Friday, so the first week of 2016 started on Sunday 27 December 2015

In this case, it's best to use UTC methods to avoid daylight saving issues:

function getWeekNumberNonISO(d) {
  // Create UTC equivalent for 23:59:59.999 on the passed in date
  var sat = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(),23,59,59,999));
  // Set to Saturday at end of week
  sat.setUTCDate(sat.getUTCDate() + 6 - sat.getUTCDay());
  // Get first day of year
  var firstDay = new Date(Date.UTC(sat.getUTCFullYear(), 0, 1));
  // Set to Sunday on or before, i.e. first day of first week in year
  firstDay = firstDay.setUTCDate(firstDay.getUTCDate() - firstDay.getUTCDay());
  // Week number is difference in dates divided by ms/week rounded
  return Math.round((sat - firstDay)/(6.048e8));
}

// Get week number for Mon 5 Jan 2015
console.log(getWeekNumberNonISO(new Date(2015,0,5))); // 2
// Get week number for Sat 31 Dec 2011
console.log(getWeekNumberNonISO(new Date(2011,11,31))); //53
// Get week number for Sat 1 Jan 2011
console.log(getWeekNumberNonISO(new Date(2011,0,1))); // 1
// Get week number for Sun 2 Jan 2011
console.log(getWeekNumberNonISO(new Date(2011,0,2))); // 2
RobG
  • 142,382
  • 31
  • 172
  • 209
-3

Js has function inbulid function which can be used to fetch the date from the given date of the week getweek().

var week=date.getWeek()
Vedvrat
  • 28
  • 5
  • 1
    erm ... no it doesn't (and that's not even a **function** you're showing) – Jaromanda X Oct 05 '17 at 01:48
  • 2
    When making claims about language features, it's always handy to include a reference to a relevant part of the [*specification*](http://ecma-international.org/ecma-262/8.0/#sec-date-objects). If you can't find one, it might not be a feature of the language… – RobG Oct 05 '17 at 02:57