-5

I am trying to get the week number of current date / specific date falls in. Any suggestions for logic building will be helpful.

Ravi Kaware
  • 1
  • 1
  • 2

2 Answers2

0
function getWeekNumber(thisDate) {
var dt = new Date(thisDate);
var thisDay = dt.getDate();

var newDate = dt;
newDate.setDate(1); // first day of month
var digit = newDate.getDay();

var Q = (thisDay + digit) / 7;

var R = (thisDay + digit) % 7;

if (R !== 0) return Math.ceil(Q);
else return Q;}

getWeekNumber("07/31/2016");

  • You should not parse strings using the Date constructor (or Date.parse, they are equivalent for parsing). It does not make sense to use Date methods to create a string to then parse to another Date (e.g. to get the first of the month), just use Date methods directly: `newDate.setDate(1)`. – RobG Jul 12 '16 at 00:19
-7
let _ = new Date,
    __ = ~~((new Date(_.getFullYear(), 0, 0) - _) / 36e5 * 24),
    ___ = (__ - __ % 7) / 7;
zhibirc
  • 180
  • 14
  • 3
    Care for additional explanation (like why you're using underscores instead of names)? – dakab Jul 11 '16 at 14:53