Why use "var d = new Date()" in this script?

Discussion in 'JavaScript' started by throntel, Jul 21, 2006.

  1. #1
    I'm new in javascript and are just checking out some stuff other people have created, I can't figure out why they need this variable for this script, but when I remove it. The script stop working.

    
    <HTML>
    <BODY>
    
    <SCRIPT STYLE = "text/javascript">
    
    var d = new Date()
    var time = d.getHours()
    
    if (time < 10)
    {
    document.write("Good morning")
    }
    
    </SCRIPT>
    
    <P>This is a simple script that will say: "Good morning" if the time is less than 10</P>
    
    </BODY>
    </HTML>
    
    Code (markup):
     
    throntel, Jul 21, 2006 IP
  2. sandossu

    sandossu Guest

    Messages:
    2,274
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    0
    #2
    what part aren`t you understanding it ?
    I`m a very newbie at js, but it`s logic
    var d = new Date()
    var time = d.getHours()

    if (time < 10)
    {
    document.write("Good morning")
    }

    time gets the time and them you check if the time it`s lower than 10. if it is, then it`s morning.
     
    sandossu, Jul 21, 2006 IP
  3. throntel

    throntel Member

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Sorry, maybe I was a bit unclear on my post :p But I'm getting what the script does, what I'm not getting is what "var d = new Date()" is needed for.
    After all the "time" variable is the only one I can see being used.
     
    throntel, Jul 21, 2006 IP
  4. giraph

    giraph Guest

    Messages:
    484
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The date variable "d" is used to create the time variable.
    
    var time = d.getHours()
    
    Code (markup):
    That d is referencing to the
    var d = new Date();
    Code (markup):
    If you only wanted one variable you could also do the following
    
    var time = new Date().getHours()
    
    Code (markup):
     
    giraph, Jul 21, 2006 IP
  5. coderlinks

    coderlinks Peon

    Messages:
    282
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The statement

    var d = new Date()

    creates a new Date object and sets it to the current time and date. Then it fetches the hour from the time and puts in the time variable. If the hour is less than 10 , that is if the time is before 10 AM, it says Good Morning.

    You can look up the properties of the Date object at:

    http://www.w3schools.com/jsref/jsref_obj_date.asp

    They also have a Javascript tutorial at:
    http://www.w3schools.com/js/

    Thomas
     
    coderlinks, Jul 23, 2006 IP
  6. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #6
    by defining variables as objects, you have access to their properties and functions... objects are neato!
     
    ccoonen, Jul 24, 2006 IP