2

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!!

HenryDev
  • 4,685
  • 5
  • 27
  • 64

3 Answers3

3

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.

Josh Beam
  • 19,292
  • 3
  • 45
  • 68
1

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()
}
Doron Sinai
  • 1,166
  • 3
  • 12
  • 28
1

Don't loop,

calculate julian date (julian date of a date is the order number of the day in the year, 32 for example is the julian date of 1st of February, see https://en.wikipedia.org/wiki/Julian_day)

Then see if current julian day is bigger or smaller than any of the birthday julian days

bbozo
  • 7,075
  • 3
  • 30
  • 56
  • According to the wikipedia page referenced in your answer Julian day is a number of days since the begining of Julian period, which is 4713 BC. It's not order number of day begining from 1st Jan of any year – Kirill Slatin Aug 16 '15 at 07:16
  • Ooops, in finance, credit card industry, they use the definition I give when they say Julian date :) – bbozo Aug 17 '15 at 08:41