Web Analytics Made Easy -
StatCounter Java Script To Find Week Day - CodingForum

Announcement

Collapse
No announcement yet.

Java Script To Find Week Day

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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)

    Comment


    • #3
      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!");
      }
      Be yourself. No one else is as qualified.

      Comment


      • #4
        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.

        Comment


        • #5
          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.
          Be yourself. No one else is as qualified.

          Comment

          Working...
          X