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 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2003
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    New Tip every day

    I have a js file on my server that contains 365 Tips. I view these tips with a javascript on my webpage. Right now the Tips are changing randomly. I want to change the server side code so that the tips change once a day for 365 days. Can anyone share the code needed to make this work? I am completely new to programming. Thanks!

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are they in an array? If so, you could do something like this:

    Code:
    function getDateInYear(dateObj) {
    	var cumulative=0;
    	for(var i = 0; i < dateObj.getMonth(); i++) {
    		if(i%2==0) cumulative += 31;
    		else if(dateObj.getMonth()==1) {
    			if(dateObj.getFullYear()%4==0) cumulative += 29;
    			else cumulative += 28;
    		} else cumulative += 30;
    	} cumulative += dateObj.getDate();
    	return cumulative;
    }
    function outputTip() {
    return tipArray[getDateInYear((new Date()))];
    }
    Hope that helps!

    Happy coding!

    Edit: Or better yet (since my script is a little off, by around two days I believe), use this
    Last edited by nolachrymose; 10-13-2003 at 01:01 PM.

  • #3
    New Coder
    Join Date
    Oct 2003
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the code. I assume that this code goes in my webpage and not in my external js file. I'm not quite sure how it works however. I will try to study it for understanding. Thanks!


    Also can you show what the Array needs to look like so that I can make sure that mine agrees? Thanks!

    Also I was confused about the edited response. I don't understand how a script that counts down to the end of the year applies. I'm just learning


  •  

    Posting Permissions

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