Skip to main content
added 195 characters in body
Source Link
Flambino
  • 33k
  • 2
  • 45
  • 90

I also have to wonder why you've created a function, which builds an array just to hold a list of incrementing numbers, which you then map to dates. The simpler way of looping through incrementing numbers is a plain old for-loop.

Anyway, here's a simple solution that adds 1 day at a time until the year rolls over, storing those daysdates that match what you're looking for:

No need to build ranges, map, and filter them. Just a while loop and push.

Incidentally, if you want to learn more about why programming calendar and time things are just hideously complex, check out this video on youtube

I also have to wonder why you've created a function, which builds an array just to hold a list of incrementing numbers, which you then map to dates. The simpler way looping through incrementing numbers is a plain old for-loop.

Anyway, here's a simple solution that adds 1 day at a time until the year rolls over, storing those days that match what you're looking for:

No need to build ranges, map, and filter them. Just a while loop and push.

I also have to wonder why you've created a function, which builds an array just to hold a list of incrementing numbers, which you then map to dates. The simpler way of looping through incrementing numbers is a plain old for-loop.

Anyway, here's a simple solution that adds 1 day at a time until the year rolls over, storing dates that match what you're looking for:

No need to build ranges, map, and filter them. Just a while loop and push.

Incidentally, if you want to learn more about why programming calendar and time things are just hideously complex, check out this video on youtube

added 330 characters in body
Source Link
Flambino
  • 33k
  • 2
  • 45
  • 90

So if you were to run this on the, say, the 30th of November, you'd actually be using the 1st of November as your start date, since the 30th's a Monday. If you run it on a Sunday, getDay will return zero, meaning your start date will be the last of the previous month. And so on.

I also have to wonder why you've created a function, which builds an array just to hold a list of incrementing numbers, which you then map to dates. The simpler way looping through incrementing numbers is a plain old for-loop.

Thirdly, don't mix calendar dates and raw time calculations like your division by milliseconds in a day. Switching back and forth between daylight savings time (aka winter/summer time) can cause all sorts of nonsense when mixing raw time and datedates. Some calendar days are 25 hours long, some are 23. And since a date constructed from only year, month, and date sets its time to midnight, you risk skipping over a date, or repeating the same date twice.

function remainingDays(weekday) {
  var current = new Date,
      year = current.getFullYear(),
      dates = [];
  
  // a simple helper function
  function nextDay(date) {
    return new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1);
  }

  // as long we're in the same year, keep adding 1 day,
  // and store the ones that match the wdayweekday we're looking for
  while(current.getFullYear() === year) {
    if(current.getDay() === weekday) {
      dates.push(current);
    }
    current = nextDay(current);
  }
  
  return dates;
}

So if you were to run this on the, say, the 30th of November, you'd actually be using the 1st of November as your start date, since the 30th's a Monday.

I also have to wonder why you've created a function, which builds an array just to hold a list of numbers, which you then map to dates. The simpler way looping through incrementing numbers is a plain old for-loop.

Thirdly, don't mix calendar dates and raw time calculations like your division by milliseconds in a day. Switching back and forth between daylight savings time can cause all sorts of nonsense when mixing time and date. Some calendar days are 25 hours long, some are 23.

function remainingDays(weekday) {
  var current = new Date,
      year = current.getFullYear(),
      dates = [];
  
  // a simple helper function
  function nextDay(date) {
    return new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1);
  }

  // as long we're in the same year, keep adding 1 day,
  // and store the ones that match the wday we're looking for
  while(current.getFullYear() === year) {
    if(current.getDay() === weekday) {
      dates.push(current);
    }
    current = nextDay(current);
  }
  
  return dates;
}

So if you were to run this on the, say, the 30th of November, you'd actually be using the 1st of November as your start date, since the 30th's a Monday. If you run it on a Sunday, getDay will return zero, meaning your start date will be the last of the previous month. And so on.

I also have to wonder why you've created a function, which builds an array just to hold a list of incrementing numbers, which you then map to dates. The simpler way looping through incrementing numbers is a plain old for-loop.

Thirdly, don't mix calendar dates and raw time calculations like your division by milliseconds in a day. Switching back and forth between daylight savings time (aka winter/summer time) can cause all sorts of nonsense when mixing raw time and dates. Some calendar days are 25 hours long, some are 23. And since a date constructed from only year, month, and date sets its time to midnight, you risk skipping over a date, or repeating the same date twice.

function remainingDays(weekday) {
  var current = new Date,
      year = current.getFullYear(),
      dates = [];
  
  // a simple helper function
  function nextDay(date) {
    return new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1);
  }

  // as long we're in the same year, keep adding 1 day,
  // and store the ones that match the weekday we're looking for
  while(current.getFullYear() === year) {
    if(current.getDay() === weekday) {
      dates.push(current);
    }
    current = nextDay(current);
  }
  
  return dates;
}
took into account RobH's comment
Source Link
Flambino
  • 33k
  • 2
  • 45
  • 90

Anyway, while calendars are complex, there's one thing that thankfully doesn't change: There are 7 days in a week. So you're creating 7 times more dates than you need, and filtering most of them away. You could consider simply skipping 7 dates at a time from the first occurrence.there's one thing that thankfully doesn't change: There are 7 days in a week. So you're creating 7 times more dates than you need, and filtering most of them away. You could consider simply skipping 7 dates at a time from the first occurrence.

Edit: The only thing in the above paragraph that holds true is that calendars are complex. As RobH points out in the comments, some pacific nations have been known to jump across the dateline. Presumably just to mess with programmers the world over!

Thirdly, don't mix calendar dates and raw time calculations like your division by milliseconds in a day. Switching back and forth between daylight savings time can cause all sorts of nonsense when mixing time and date. Some calendar days are 25 hours long, some are 23.

Anyway, there are two ways you can go. Either find the first occurrence of the day you're looking for, and then repeatedly add 7 days until the year rolls over; or addhere's a simple solution that adds 1 day at a time until the year rolls over, storing those days that match what you're looking for.

This is the latter, since it's shorter:

Anyway, while calendars are complex, there's one thing that thankfully doesn't change: There are 7 days in a week. So you're creating 7 times more dates than you need, and filtering most of them away. You could consider simply skipping 7 dates at a time from the first occurrence.

Thirdly, don't mix calendar dates and raw time calculations like your division by milliseconds in a day. Switching back and forth between daylight savings time can cause all sorts of nonsense when mixing time and date. Some calendar days are 25 hours long, some are 23.

Anyway, there are two ways you can go. Either find the first occurrence of the day you're looking for, and then repeatedly add 7 days until the year rolls over; or add 1 day at a time until the year rolls over, storing those days that match what you're looking for.

This is the latter, since it's shorter:

Anyway, while calendars are complex, there's one thing that thankfully doesn't change: There are 7 days in a week. So you're creating 7 times more dates than you need, and filtering most of them away. You could consider simply skipping 7 dates at a time from the first occurrence.

Edit: The only thing in the above paragraph that holds true is that calendars are complex. As RobH points out in the comments, some pacific nations have been known to jump across the dateline. Presumably just to mess with programmers the world over!

Thirdly, don't mix calendar dates and raw time calculations like your division by milliseconds in a day. Switching back and forth between daylight savings time can cause all sorts of nonsense when mixing time and date. Some calendar days are 25 hours long, some are 23.

Anyway, here's a simple solution that adds 1 day at a time until the year rolls over, storing those days that match what you're looking for:

Source Link
Flambino
  • 33k
  • 2
  • 45
  • 90
Loading