Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    zk0
    zk0 is offline
    New Coder
    Join Date
    Dec 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Days until my birthday script

    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:

    Code:
    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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Have a look at

    http://javascript.internet.com/time-...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))
    Last edited by Philip M; 02-19-2007 at 01:02 PM.

  • #3
    zk0
    zk0 is offline
    New Coder
    Join Date
    Dec 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okey, I have updated the code. It now looks like this. What have I done wrong that makes the code not to write out?

    Code:
    	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( ) )

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    <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.
    Last edited by Philip M; 02-19-2007 at 08:31 PM.

  • #5
    zk0
    zk0 is offline
    New Coder
    Join Date
    Dec 2006
    Posts
    51
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you Philip!

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

    Code:
    	// 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... :(" )
    Quote Originally Posted by Philip M View Post
    <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.
    Last edited by zk0; 02-20-2007 at 08:15 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •