JavaScript How to loop through the remaining days of the year and look for certain dates in javascript?

I have 3 people's birthdays:

Steve -> May 3

Mark -> October 20

Robbin -> December 5

How to loop through the remaining days of the year and find which person still has a birthday coming (IN THIS CASE output MARK AND ROBBIN)?

I've been able to get the remaing days of the year, but I'm not sure how to loop through the remaing days of the year and output those people's names (the ones that will have bithdays in the next months). This is what I have done so far:

I created a funtion to get the remaming days:

function remainingDays(date1, date2) {
// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24

var date1_ms = date1.getTime()
var date2_ms = date2.getTime()

var difference_ms = Math.abs(date1_ms - date2_ms)

// Convert back to days and return
return Math.round(difference_ms/ONE_DAY)
}

As you can see the function returns the number of days left. And here's the values:

var current_date = new Date()

// Store the date of the next New Year's Day
var new_years_date = new Date()
new_years_date.setYear(new_years_date.getFullYear() + 1)
new_years_date.setMonth(0)
new_years_date.setDate(1)

// Call the remainingDays function
var days_left = remainingDays(current_date, new_years_date);

if (days_left > 1) {
document.write(days_left + " days left this year")
} else {
 document.write(days_left + " day left this year")
 }

Please help me....Thank you in advanced!!

Answer:1

What about something like this? (see the jsfiddle):

var birthdays = [
  {
    name: 'Mark',
    date: 'October 20'
  },
  {
    name: 'Robbin',
    date: 'December 5'
  }
];

var upcoming = birthdays.filter(function(person) {
  var date = new Date(person.date + ' 2015');

  // returns a boolean "true" or "false"
  return date > new Date();
});

console.log(upcoming);

It simply filters the current birthdays into a new array, upcoming, if the birthday is greater than today's date. No need to overcomplicate it.

You could make it more semantic and extensible if you so desire:

var upcoming = birthdays.filter(afterToday);

function afterToday(person) {
  return new Date(person.date + ' 2015') > new Date();
}

One of the key paradigms of programming is that simpler is better. Many algorithms can do the same thing (end up with the same results), but usually the simpler algorithm is better, all else being equal.

In other words:

  1. The algorithm can loop through every single date remaining in the year, or

  2. The algorithm can simply check if the date is greater than today

I think the second is simpler. Also, less lines of code, and more readable.

Answer:2

You can set a variable for each person and check if his birthday is coming up

var Mark = new Date(2015, 9, 20);
var now = new Date()

if(Mark > now){
doSomething()
}
Answer:3

I'm trying to load an external script while using Meteor. Currently using this code in my layout.js to some success. Meteor.startup( function() { $.getScript('js/scripts.js'); }); However, if I go ...

I'm trying to load an external script while using Meteor. Currently using this code in my layout.js to some success. Meteor.startup( function() { $.getScript('js/scripts.js'); }); However, if I go ...

  1. jquery loading external html

Let's say we have an array that can have a length of 1 to ?? We have a way of navigating this array up or down by clicking a button that either navigates upward or downward, which is displayed in the ...

Let's say we have an array that can have a length of 1 to ?? We have a way of navigating this array up or down by clicking a button that either navigates upward or downward, which is displayed in the ...

  1. index array bounds matlab
  2. index outside bounds array c#
  3. index outside bounds array
  4. index out bounds array
  5. index exceeds array bounds
  6. index exceeds array bounds matlab
  7. index exceeds array bounds matlab error
  8. index exceeds array bounds title matlab
  9. index exceeds array bounds matlab meaning
  10. index exceeds array bounds meaning
  11. index exceeds array bounds xlabel
  12. index exceeds array bounds simulink
  13. index exceeds array bounds matlab plot
  14. index out of bounds array c#
  15. index exceeds array bounds parfor
  16. index outside bounds of array sql server
  17. sql index outside bounds array
  18. array index bounds of exception
  19. index outside bounds of array error
  20. array index bounds exception java

I currently have a button for each language to allow users to view website in difference languages <a href="http://www.jthink.net/songkong/en" title='English'><img src="http://static....

I currently have a button for each language to allow users to view website in difference languages <a href="http://www.jthink.net/songkong/en" title='English'><img src="http://static....

  1. load html page in div
  2. load html page from javascript
  3. load html page on button click
  4. load html page into another html page
  5. load html page in div using jquery
  6. load html page into div onclick
  7. load html page using jquery
  8. load html page using javascript
  9. load html page from assets android
  10. load html page in php
  11. load html page in webview android
  12. load html page in node js
  13. load html page in iframe
  14. load html page in react
  15. load html page in another html page
  16. load html page in wpf
  17. load html page in popup jquery
  18. load html page in python
  19. load html page in bootstrap modal
  20. load html page in div using ajax

I have 4 check boxes, and depending on which one/pair is selected I want a specific array to be displayed on the page. I'm relatively new to angularjs so I'm not sure how to get this to work. So how ...

I have 4 check boxes, and depending on which one/pair is selected I want a specific array to be displayed on the page. I'm relatively new to angularjs so I'm not sure how to get this to work. So how ...

  1. angularjs display array of objects
  2. angularjs display array in html
  3. angularjs display array of strings
  4. angularjs display array list
  5. angularjs display array
  6. angularjs display array in table
  7. angularjs display array index
  8. angularjs display array data
  9. angularjs display json array
  10. angularjs display count of array
  11. angularjs display image byte array
  12. angularjs display element of array