JS Tutorial: JavaScript Date Object usage
Let's take a look at an example and return the current date and time.
<body>
<script type= "Text/javascript" >
document.write (Date ());
</script>
</body>
Example two gettime ()
<body>
<script type= "Text/javascript" >
var minutes = 1000*60;
var hours = minutes*60;
var days = hours*24;
var years = days*365;
var d = new Date ();
var t = d.gettime ();
var y = t/years;
document.write ("It ' s been:" + y + "years since 1970/01/01!");
</script>
</body>
Results.
It ' s been:38.89412106846144 years since 1970/01/01!
Okay, let's look at the date tutorial '
Finish Date Object reference
For a complete reference of all the properties and methods that can be used with date objects, we go to complete the Date object reference.
Mention contains a short description and examples of each of the properties and methods used!
Create a Date Object
The Date object is used with the date and time.
The following code creates a Date object MyDate requirement:
var mydate=new Date ()
Note: The Date object will automatically have the current day and time as its initial value! Set the date we can easily manipulate the date of the use method can be used on the day object. In the following example, we set a Date object's specific date (January 14, 2010): Var mydate=new date ();
Mydate.setfullyear (2010,0,14); In the following example, we set a Date object that is 5 days into the future: Var mydate=new date ();
Mydate.setdate (Mydate.getdate () +5); Note: If an increase of 5 days, shift date one months or a year, the change is automatically processed by the Date object itself! Dates that compare two date objects are also used to compare two dates. The following example compares today's date with January 14, 2010 var mydate=new date ();
Mydate.setfullyear (2010,0,14); var today = new Date (); if (Mydate>today)
{
Alert ("Today is before 14th January 2010");
}
Else
{
Alert ("14th January 2010");
}