Basic JavaScript Date and Time Functions

As the title of post implies, in this tutorial i will show you how to get the date and time from the visitor's computer using an example.here you will get the the basic information about how to create a date object in javascript.

How To Create  a Date Object
When a date object is created, it is stored in what's called a variable.Below is the syntax to create a date object.
var date = new Date();
Example:The code given below will show the today date and time from client computer. 
<script language="javascript">
function myFunction(){
    var date = new Date();
    alert(date.toString());
 
}
</script>
<input type="button" onclick="myFunction();" value="Today's Date Is" />

Click on this button will display datetime from your computer.
How To Extract Information From that Date Object
Lots of information can be extracted from a date object. In this tutorial, we'll extract only some important info:
   
    var hour        = date.getHours();
    var minute      = date.getMinutes();
    var second      = date.getSeconds();
    var monthnumber = date.getMonth();
    var monthday    = date.getDate();
    var year        = date.getYear();
Tags: , ,

Join Us!