...

View Full Version : Days until my birthday script



zk0
02-19-2007, 12:32 PM
Hi everyone!

I need some help with this javascript. I just don't seem to understand how to code it properly.

The code is suppose to let the user enter which month his birthday is on and which day, then the script would tell the user how many days it is until his birthday. But I don't know how to code that. This is what I need help with:

I know that if I count todays ms seconds subtraction with the ms for the users birthday and then convert that to days I will know how many days it's left to the users birthday.

This is the code I got so far:


var minusEtt = Number( prompt('What month is your birthday (1-12)?','') );
var date = Number( prompt('What day is your birthday (1-31)?','') );

var month = minusEtt - 1 ; // the month - one

var nextBirthday = new Date( month, date, 0, 0, 0, 0 ) // month, date, hour, min, sec, ms

var todaysDate = new Date(); // Todays date

Philip M
02-19-2007, 01:59 PM
Have a look at

http://javascript.internet.com/time-date/days-between-two-dates.html

or adapt

var d1 = Date.parse("Feb 18, 2006"); // date 1.
var d2 = new Date(); // date 2, in this case todays date.
document.write(Math.floor((d2.getTime() - d1)/86400000))

zk0
02-19-2007, 07:09 PM
Okey, I have updated the code. It now looks like this. What have I done wrong that makes the code not to write out?


var minusEtt = Number( prompt('What month is your birthday (1-12)?','') );
var date = Number( prompt('What day is your birthday (1-31)?','') );

var month = minusEtt - 1 ; // the month - one

var nextBirthday = new Date( month, date, 0, 0, 0, 0 ) // month, date, hour, min, sec, ms

var todaysDate = new Date(); // Todays date

myDate.getTime();

var skillnad = nextBirthday.getTime() - todaysDate.getTime();

var skrivUt = skillnad / 1000 / 60 / 60 / 24 ;

document.write( skrivUt.toString( ) )

Philip M
02-19-2007, 08:33 PM
<SCRIPT>

var mm = Number( prompt('What month is your birthday (1-12)?','') );
mm=mm-1;
var dd = Number( prompt('What day is your birthday (1-31)?','') );
var d2 = new Date(); //todays date.
var bday = new Date();
var yy = bday.getFullYear();
bday.setDate(dd);
bday.setMonth(mm);
bday.setFullYear(yy+1);
var result=(Math.floor((bday.getTime() - d2)/86400000))
if (result>=365) {result=result-365}
document.write(result);

</SCRIPT>

Note you need to add checks that the month is specified as 1-12 and the day is a value appropriate to that month, e.g. not February 31st.

Also be aware that the calculated number of days includes today. So if today is February 19th and the birthday is February 20th then the result is 1 day (but 0 clear days). Tomorrow will return 0 days. But it can be argued that tomorrow is 365 days from the next birthday.

zk0
02-20-2007, 08:59 PM
Thank you Philip!

I've changed the code to fit me and my goals better:


// MOMENT 1 BÖRJAR HÄR -------------------------------------------

var minusEtt = Number( prompt('Vilken månad fyller du år (1-12)?','') );
var date = Number( prompt('Vilken dag fyller du år (1-31)?','') );

var month = minusEtt - 1 ;

// MOMENT 2 BÖRJAR HÄR -------------------------------------------

var todaysDate = new Date(); // Dagens datum

var nextBirthday = new Date(); // Nästa födelsedags datum

var year = nextBirthday.getFullYear();
nextBirthday.setDate(date);
nextBirthday.setMonth(month);
nextBirthday.setFullYear(year + 1);

var skrivUt=(Math.floor((nextBirthday.getTime() - todaysDate)/86400000))

// MOMENT 3 BÖRJAR HÄR -------------------------------------------

if (skrivUt>=365) {skrivUt=skrivUt-365}
document.write( "Det är " + skrivUt.toString() + " dagar kvar till din födelsedag. " )

if (skrivUt==0)
document.write( "GRATTIS! :)" )

if (skrivUt==364)
document.write( "Ush vad trist... :(" )


<SCRIPT>

var mm = Number( prompt('What month is your birthday (1-12)?','') );
mm=mm-1;
var dd = Number( prompt('What day is your birthday (1-31)?','') );
var d2 = new Date(); //todays date.
var bday = new Date();
var yy = bday.getFullYear();
bday.setDate(dd);
bday.setMonth(mm);
bday.setFullYear(yy+1);
var result=(Math.floor((bday.getTime() - d2)/86400000))
if (result>=365) {result=result-365}
document.write(result);

</SCRIPT>

Note you need to add checks that the month is specified as 1-12 and the day is a value appropriate to that month, e.g. not February 31st.

Also be aware that the calculated number of days includes today. So if today is February 19th and the birthday is February 20th then the result is 1 day (but 0 clear days). Tomorrow will return 0 days. But it can be argued that tomorrow is 365 days from the next birthday.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum