#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    5

    New/learning javascript, hoping for clarification for this short piece of code please


    Hello all,

    Im new to Javascript so i'm trying to learn from the beginning. i wrote this, as a first attempt, but no joy. can anyone help point me in the right direction/ guide me on what im doing wrong please.

    Code:
    <head>
        <title>Untitled Page</title>
        <script type="text/javascript">
    
            function myfunction() {
                var d = new date();
                var day = d.getDate();
                if (day == 2) {
                    alert("Tuesday")
                }
                else { 
                    alert("some other day")
                }
            }
    
            
        </script>
    </head>
    <body>
    
    <button type="button" onclick="myfunction()"> click me</button>
    
    </body>
    </html>
    Your help as always is greatly appreciated.

    Kind regards
    MG
  2. #2
  3. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,008
    Rep Power
    2790
    "getDate" returns the day of the month, i.e. 1 to 31. "getDay" will return 0 to 6 for the day of the week.
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

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

    I changed it to 'getDay', but that doesnt work either

    Code:
            function myfunction() {
                var d = new date();
                var day = d.getDay();
                if (day == 2) 
                {
                    alert("Tuesday")
                }
                else 
                { 
                    alert("some other day")
                }
            }
    regards
    MG
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    What if you wrote
    var d = new Date();
    with capital D?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    5
    Hi, thanks for the post.

    So i changed it slightly and now its kind of working. the new code looks like this:

    Code:
               var d = new date();
                var day = d.getDay();
            function myfunction() {
                
                if (day == 2) 
                {
                    alert("Tuesday");
                }
                else 
                {
                    alert("some other day");
                }
            }
    As you can see the variables are now outside the function, but i thought that shouldnt matter? you can put variable in functions right?

    but now it displays "some other day" ? isnt tuesday the number 2 from 0-6 using the getDay() method??

    Kind regards
    MG
    Last edited by mind_grapes; October 2nd, 2012 at 06:26 AM.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    Yes, you can, and, maybe should, define variables inside functions!
    I've tried your code in my browser, and replaced
    new date();
    with
    new Date();

    It complained "date is not defined"; changed it to Date - happy browser.

    best
    jwer
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    5
    Hello, cheers for reply.

    I changed it like yourself, and now it works. I think it may have been due to it not being a capital 'D'. Caps hey!? :-).

    thank you all for your help. You've been a real help as always.

    Regards
    MG

IMN logo majestic logo threadwatch logo seochat tools logo