Products
Services
Contact Us

Scripts: Javascript :: Date and Time Functions :: Library Article #13

Developer's Section

Get today's date using Javascript
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating:  | Rate It:   
Average Votes: (3198)
Favorites:

Learn to get today's date using javascript's Date Object and its applicable methods.

We can use an object of type Date to determine today's date. This object is standard for most browsers and will give you the information according to your current computer's date.

The following is an example on how to get the date for today and output the result in a small window:



Now let's take a look at how we actually used the Date Object and what methods we called in the source code:

 Code Snippet 1

<script language="Javascript">

function getTodaysDate() {
     
     //Create a Date Object and call calendar methods:
     
    var todaysDate = new Date()
    var month = todaysDate.getMonth() + 1
    var day = todaysDate.getDate()
    var year = todaysDate.getFullYear()
    var hour = todaysDate.getHours()
    var minutes = ""
    var temp = todaysDate.getMinutes()
    if(temp < 10){minutes += "0"}
    minutes += temp;
    var seconds = todaysDate.getSeconds()
    var amPmIndicator = ""
    if(hour > 11){ amPmIndicator = "PM" } else { amPmIndicator = "AM"  }

    var now = month + "/" + day + "/" + year + " "
        now += hour + ":" + minutes + ":" + seconds + " " + amPmIndicator
         
    alert(now)   
}
</script>


Lastly, we can create a button to test the actual function:

 Code Snippet 2

<input type="button" name="btnex1" value="Click to find out Current Date" 
 onclick="javascript:getTodaysDate()">


See other Scripts in Date and Time Functions

 

Submit Your Scripts:

If you would like to have your Javascripts published in this section please fill out
the form below:
*Your Name or Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]