JS Tutorial: JavaScript Date Object usage

Source: Internet
Author: User

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");
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.