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!!
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:
The algorithm can loop through every single date remaining in the year, or
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.
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()
}
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 ...
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 ...
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....
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 ...