Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    6

    This js code doesn't seem to work and i don't know why.


    Hi all, how are you, hope you can help.

    New to Javascript, but the other day I wrote the following js code, but it didn't work and I can't work out why this might be the case.

    The code should output a number (17 or 16) based on the number returned by the getDate() method. It worked for 28 (it returned the number 17) but the next day it failed to work.

    Code:
    var date = new Date();
            var dayNumber = date.getDate(); 
            function theDay() {
    
                if (dayNumber === 28) {
                    document.getElementById("day").innerHTML = 17;
                }
                else if (dayNumber === 29) {
                    document.getElementById("day").innerHTML = 16;
                }
                else 
                {
                    document.getElementById("day").innerHTML = "Not a valid day";
                }
            }
    I hope someone can help explain in why this was then case.

    Kind regards
    MG
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    You're not really asking us why your JavaScript didn't use the right day of the month last Friday? How on earth could we know that? Do you expect us to travel back in time and check your system clock?

    How about checking today if the code outputs the right day?
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    50
    Rep Power
    35
    Working for me here: jsFiddle
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    6
    Hi thanks for your reply.

    I really do appreciate people taking time away from their daily routine to help people, such as me, people that do not know what they're doing, try to improve one small baby step at a time. I'm not sure how I've offended you, but if i missed something off the original post, please say and i'll do my best to try to bring you the required information. Perhaps I didn't correctly explain myself or ask the appropriate question.

    I checked it today and it worked; as it worked last Thursday. There wasn't anything wrong with my system back then, nor is there today.

    The output last week - on thursday - was 17, on my work pc and home pc. However, on Friday of last week - Friday the 29th of March - as soon as the clock stuck midnight the 17 disappeared - as it should have - but the 16 didn't appear. It remained blank.

    Perhaps this should have been included in my original question. Why would the code above work now - and last thursday - and come midnight stop displaying altogether?

    Once again, thanks to all who contribute and read.

    Kind regards
    MG
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    6
    Hi thanks, for the reply.

    It worked for me too: as in it worked for today and last thursday. However, as the clock strikes midnight, the number disappears, with the next number appearing.

    Can you see anything in the code that suggests it's not written correctly / may cause it to stop working?

    Thanks for your post

    Regard
    MG
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Sorry for the reaction, but sometimes we get very weird questions, and yours sounded a bit like that at first.

    OK, obviously you left out some info. So this code runs periodically and not just once? Have you checked if the "number switch" works at all? Because if you get nothing, it's obviously a problem of the switch logic and has nothing to do with this function. Because the function will fill #day in any case, even if no condition is met (which yields "Not a valid day").

    Instead of days, try minutes. Then you don't have to wait for midnight again.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  12. #7
  13. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,131
    Rep Power
    4304
    MG, how are you calling theDay and why did you define dayNumber outside of it? (It's best to avoid using global variables in JavaScript.)

    @Jacques1 I agree that it's a somewhat odd request. Is it that hard to specify a date in the call to the date constructor instead of using the default of the current day's date?
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    6
    @ jacques it's fine. It was important to say, and I did leave that off - which was my error, sorry. I respect your apology :-) I do struggle with this Javascript business.

    Yeah, periodically is right. I'm not sure what you mean by 'number switch'. Are you referring to case switches? In that, I should try using those to alert the correct number?

    ha, I did wait till midnight. I'm not sure what to write and how to write it. how would test to see if it works for the next day if you did it by minutes? is it not logical to bring back the day by using the getDate() method as this will return the number between 1-31? and then you can output the number - 17 or 16, 15 etc - you want to display.

    thanks folks

    Kind regards
    MG
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    6
    @ kravvitz, cheers for the reply.

    In truth i dont know why i made them global variables. I just started writing and at first it worked so they remained there. I'll move them into the function.

    im calling theday() by using onload, so when someone first goes to the page it loads the function. is that correct?

    Thank you
    Regards
    MG
  18. #10
  19. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,131
    Rep Power
    4304
    Sure, using onload is fine.

    The function is written to always output something, so I don't see how it could not output anything on the 29th. There must be something else involved. I think we may need to see the page you're testing this function in.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,027
    Rep Power
    377
    i tested and there is nothing wrong with switch.. it should always output something as kravvitz has said. I tested few different values (between 1 & 30) just to make sure. so the problem as he also said must be somewhere else.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    6
    Morning all, thanks for the replies.

    You can find the homepage for where the script was due to be implemented here .

    There is currently an image at the top of the page. This was used as soon as I realized the script didn't work. However, using web developer tools, you can see my original script is still in the head section - the variables have now been made local to the function as per @ kravvitz's suggestion.

    Is it possible the reason it didn't work was due to the variables not being local, within the function.

    I'm thinking of removing the image and trying again, now that the variables have been made local.

    Regards
    MG
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    6
    Hello all,

    I've just been playing with the code and noticed something.

    If you start the script off with the day we're currently on - so today is the 4 - the valued returned is the value we want, so in this case 13. However, if you then move today - the 4th - to second in the 'else if' statement, and the first is now yesterday - the 3rd - the message displayed to the user is "date not valid". Any thoughts as to why this is the case?

    i'll try to use the switch statement as you've suggested and get back to you.

    Kind regards
    MG
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    50
    Rep Power
    35
    Surely it would be easier to have theDay function calculate the number of days to a specified date rather than hard code the remaining days using if statements, something like:
    Code:
    function theDay(eventDate) 
    {                        
        var daysToGo = eventDate - new Date().getDate();
        document.getElementById("day").innerHTML = daysToGo;
    }
    
    theDay(17);
    You can see it working here: http://jsfiddle.net/TEaYu/

    Edit: Update to allow for a countdown across any length of time:
    Code:
    function theDay(eventDate) 
    {
        var millisecDiff = eventDate - new Date();
        document.getElementById("day").innerHTML = Math.round(millisecDiff/1000/60/60/24);
    }
    
    theDay(new Date(2013, 3, 17)); // 17th April 2013, month is zero based hence April is 3
    Can be seen here: http://jsfiddle.net/TEaYu/1/
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    6
    Hello,

    I've just looked at case switches, and it worked. It worked when the current day was the first switch, and also when it was pushed down the list.

    I'll try this and see if all is ok come midnight.

    Thanks for your help. i'll check back tomorrow and let you know how it went.

    Regards
    MG
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo