I'm trying to calculate remaining days until the next birthday but keeps running into problems. This is the code so far (has some issues with negative days if your birthday has already been this year etc). Any tips on how I can solve this?
var birthdayDate = new Date(year, month-1, day, 12);
var now = new Date();
var days = 0;
Math.floor(days = ( (birthdayDate.getTime() - now.getTime()) / (1000*60*60*24)));
if(days < 0)
{
var yearDiff = birthdayDate.getYear() - now.getYear();
yearDiff *= -1;
var monthDiff = birthdayDate.getMonth() - now.getMonth();
var daysDiff = birthdayDate.getDay() - now.getDay();
if(monthDiff <= 0)
{
if(daysDiff > 0)
{
}
else
{
days += 365;
}
}
var extraDays = yearDiff / 4;
days = days + (yearDiff * 365) + extraDays;
}
else
{
throw new FutureDateException();
}
days = Math.floor(Math.round(days));
if(days === 365)
{
days = 0;
}
if(days === 366)
{
days = 1;
}
return days;
I'm trying to calculate remaining days until the next birthday but keeps running into problems. This is the code so far (has some issues with negative days if your birthday has already been this year etc). Any tips on how I can solve this?
var birthdayDate = new Date(year, month-1, day, 12);
var now = new Date();
var days = 0;
Math.floor(days = ( (birthdayDate.getTime() - now.getTime()) / (1000*60*60*24)));
if(days < 0)
{
var yearDiff = birthdayDate.getYear() - now.getYear();
yearDiff *= -1;
var monthDiff = birthdayDate.getMonth() - now.getMonth();
var daysDiff = birthdayDate.getDay() - now.getDay();
if(monthDiff <= 0)
{
if(daysDiff > 0)
{
}
else
{
days += 365;
}
}
var extraDays = yearDiff / 4;
days = days + (yearDiff * 365) + extraDays;
}
else
{
throw new FutureDateException();
}
days = Math.floor(Math.round(days));
if(days === 365)
{
days = 0;
}
if(days === 366)
{
days = 1;
}
return days;
Share
Improve this question
asked Jan 13, 2014 at 11:31
MaGiMaGi
871 gold badge1 silver badge9 bronze badges
4 Answers
Reset to default 8I'd do something like this:
var myBirthday, today, bday, diff, days;
myBirthday = [6,2]; // 6th of February
today = new Date();
bday = new Date(today.getFullYear(),myBirthday[1]-1,myBirthday[0]);
if( today.getTime() > bday.getTime()) {
bday.setFullYear(bday.getFullYear()+1);
}
diff = bday.getTime()-today.getTime();
days = Math.floor(diff/(1000*60*60*24));
alert(days+" days until Niet's birthday!");
To return the correct number of days,
regardless of daylight savings time use Math.round rather than Math.floor,
and set both dates time to midnight.
function daysUntilNext(month, day){
var tday= new Date(), y= tday.getFullYear(), next= new Date(y, month-1, day);
tday.setHours(0, 0, 0, 0);
if(tday>next) next.setFullYear(y+1);
return Math.round((next-tday)/8.64e7);
}
//test 1
var d= daysUntilNext(12, 25);
if(d=== 0) alert('Merry Christmas!');
else alert(d+' day'+(d>1? 's': '')+' until Christmas');
//test2
var d= daysUntilNext(4, 26);
if(d=== 0) alert('Happy Birthday!');
else alert(d+' day'+(d>1? 's': '')+' until your birthday');
// Your birthday
var birthday = new Date(1985, 0, 1);
var today = new Date();
//Set current year or the next year if you already had birthday this year
birthday.setFullYear(today.getFullYear());
if (today > birthday) {
birthday.setFullYear(today.getFullYear() + 1);
}
//Calculate difference between days
Math.floor((birthday - today) / (1000*60*60*24))
For any date calculations in JS, I suggest using date.js library. JS has very strange date/time concepts :)
vijay birthday calculate in days
function thalapathyBirthday(month, day) {
var manos = new Date(),
vijay = manos.getFullYear(),
next = new Date(vijay, month - 1, day);
manos.setHours(0, 0, 0, 0);
if (manos > next) next.setFullYear(vijay + 1);
return Math.round((next - manos) / 8.64e7);
}
var vajasan = thalapathyBirthday(06, 22);
if (vajasan === 0) console.log('Happy Birthday Thalapathy!');
else console.log(vajasan + ' day' + (vajasan > 1 ? 's' : '') + ' to go thalapathy birthday');