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
    New Coder
    Join Date
    Jan 2011
    Location
    Kochi, Kerala, India
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Arrow Java Script To Find Week Day

    // since we can't work with the "actual" Date Object we need to create an instance of it (a copy)
    // fill the variable now with the current date now is a snap-shot of the Date Information

    Code:
    var now = new Date();
    
    // getDay() returns the day of the week as an integer from 0 (Sunday) to 6 (Saturday).
    // by using &&, logical operator, we're checking to see if both parts are true
    
    if (now.getDay() > 0 && now.getDay() < 6) {
         document.write("Sorry, it's a weekday.");
    }
    else {
         document.write("Hooray, it's a weekend!");
    }
    
    -- OR --
    
    if (now.getDay() > 0 && now.getDay() < 6) document.write("Sorry, it's a weekday.");
    else document.write("Hooray, it's a weekend!");

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Posts
    725
    Thanks
    35
    Thanked 132 Times in 123 Posts
    If you check the modulus of the day integer against 6 you can match both 0 and 6 in one statement.

    6%6 and 0%6 are both equal to zero.

    Code:
    var weekendstring= new Date().getDay()%6 ? 
    "Sorry, it's a weekday.":"Hooray, it's a weekend!";
    
    document.write(weekendstring)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Nice trick, MrHoo.

    Now the real question: Why the heck did "index" post that in the first place?

    On top of which, his comment at the top is *WRONG*.
    // since we can't work with the "actual" Date Object we need to create an instance of it (a copy)
    NONSENSE!

    MrHoo's code proved otherwise. There's no "copy" of an "actual" Date object in his code, at all!

    And even if you don't use his trick:
    Code:
    if ( (new Date()).getDay() > 0 && (new Date()).getDay() < 6) {
         document.write("Sorry, it's a weekday.");
    }
    else {
         document.write("Hooray, it's a weekend!");
    }
    Granted, it may make more sense to assign it to a variable if you use that code, but "can't work with" is clearly not true.

    And besides, if you are going to use that code, it would make more sense to do:
    Code:
    var day = (new Date()).getDay();
    if (day > 0 && day < 6) {
         document.write("Sorry, it's a weekday.");
    }
    else {
         document.write("Hooray, it's a weekend!");
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,731
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    I have the idea that
    // since we can't work with the "actual" Date Object we need to create an instance of it (a copy)
    is supposed to mean that you cannot use the information in the Date Object (say getTime()) without creating an instance of it as var now = New Date(). Hence now is a snap-shot of the Date Information. Not well phrased and a bit obvious.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    And here I thought *I* was the pedant!

    Yes, I suppose that, at some level, all that's true.

    In actuality, there is no such thing as a "Date object" until you create one. There is only a Date constructor (the constructor is a function, and in JS even functions are objects, but let's not travel that slippery path). So indeed you must do new Date() to invoke the constructor to create the object that we can *then* call "an instance of a Date object."

    This all comes about because JavaScript refused to entertain the notion of "classes" and insisted on calling everything objects. In any other language, we'd say there really *IS* no such thing as a Date object. There would be a Date class, and then you could create an object REFERENCE that refers to an instance of the Date class, which is an object (but not, technically, a Date object).

    But JS decided to lump everything under one name. Ugh. A sloppy language. It's fun, but it's still sloppy.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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