Use of Date objects in JavaScript __java

Source: Internet
Author: User
Tags european date format getdate time zones local time

JavaScript does not have a basic date data type, so we can only explicitly create the Date object. We create a new Date object and create a string object the same way, using the keyword new and date constructors. The following line creates a Date object that contains the current date and practice:

var todaysdate = new Date ();

To create a Date object that stores a specific date or time, we can simply put the date or date and time inside the brackets:

var newmillennium = new Date ("1 off 2000 10:24:00");

Different countries use different sequences to describe dates. For example, the date format specified by the United States is mm/dd/yy, and the European date format is DD/MM/YY, in China their format is YY/MM/DD. If you specify the month by using the abbreviated name, you can use it in any order:

var somedate = new Date ("2002");

var somedate = new Date ("10 2002");

var somedate = new Date ("2002");

In fact, a Date object can have many parameters:

var somedate = new Date (ayear, AMonth, Adate,➥

Anhour, Aminute, Asecond, Amillisecond)

To use these parameters, you first need to specify the year and month, and then use the parameters you need, but you have to use them sequentially and cannot choose between them. For example, you can specify the year, month, date, and hour:

var somedate = new Date (2003, 9, 22, 17);

But you can't specify the year, month, and then specify the hour:

var somedate = new Date (2003, 9,, 17);

Note: Although we generally consider September (September) to be the 9th month, JavaScript calculates the month from 0 (January), so the month 8 represents September.

2.1.1.1.1. using the Date object

The Date object has many methods that you can use to get or set a date or time. You can use local time (the time in your computer's time zone) or UTC (Global standard Time, also called Greenwich Mean Time). While this can be very useful, realize that when you process date, many people do not set their time zones correctly. Let's look at an example that shows some of these methods:

<body>

<script type= "Text/javascript" >

Create a new Date object

var somedate = new Date ("2003 11:59");

Retrieve the four values using the

Appropriate Get methods

document.write ("Minutes =" + somedate.getminutes () + "<br>");

document.write ("year =" + somedate.getfullyear () + "<br>");

document.write ("Month =" + somedate.getmonth () + "<br>");

document.write ("Date =" + somedate.getdate () + "<br>");

Set the minutes to 34

Somedate.setminutes (34);

document.write ("Minutes =" + somedate.getminutes () + "<br>");

Reset the date

Somedate.setdate (32);

document.write ("Date =" + somedate.getdate () + "<br>");

document.write ("Month =" + somedate.getmonth () + "<br>");

</script>

</body>

Here are the results you'll get:

Minutes = 59

Year = 2003

Month = 0

Date = 31

Minutes = 34

Date = 1

Month = 1

The first glance of this line of code may seem a bit counterintuitive:

Somedate.setdate (32);

JavaScript knew that there were no 32 days in January, so the interpreter didn't try to set the date to 32 days, but it counted 32 days from January 1, so we returned February 1.

This is a very handy feature if you need to add some days to a date. Usually if we want to add many days to a date, we have to think about the number of days in different months and whether it's a leap year, but it's very easy if we use the date of JavaScript to process it:

<body>

<script type= "Text/javascript" >

Ask the user to enter a date string

var originaldate = prompt ("Enter a Date" (Day, Name Of➥

The Month, year) "," 2003 to Dec ");

Overwrite the originaldate variable with a new Date

Object

var originaldate = new Date (originaldate);

Ask the user to enter the number of

added, and convert to number

var addDays = number (Prompt ("Enter Number of Days➥

To be added "," 1 "))

Set a new value for OriginalDate of OriginalDate

Plus the added

Originaldate.setdate (originaldate.getdate () + addDays)

Write out of the date held by the OriginalDate

Object using the ToString () method

document.write (originaldate.tostring ())

</script>

</body>

If you enter 2003 (December 31, 2003) at the prompt, and 1 as the number of days to be added, then you will get the result of the Thu 1 00:00:00 utc+0800 2004 (Thursday, January 1, 2004, Horizon 0 minutes 0 seconds).

N Note : Note that in the third line of the script, we used the number () method of the Math object. The program works well if we don't use it, but the results will be different. If you don't want to use this method to convert different data types, then there's a trick: a string that uses parseint (), parsefloat (), or number () to convert to numbers, and if you subtract 0 from it, you convert it to a number, If you add an empty string to a number, ' then you convert it to a string, which usually needs to be handled with ToString ().

In line Fourth of the code, we set the value returned by the date originaldate.getdate () plus the number of days that need to be added, and the last line uses the ToString () method to output the date originaldate the Date object in the form of a string. If you are using a IE5.5 version or a Gecko browser (Mozilla, Netscape 6 or above), using the todatestring () function of the date alone produces a well-formed string. You can use the same method to get and set the date. If you are using UTC time, all you need to do is add UTC to the method name. So gethours () becomes getutchours (), Setmonth () becomes setutcmonth (), and so on. You can also use the getTimezoneOffset () method to return a small time difference between your computer's local and UTC times. (You have to rely on users to set their time zones correctly and realize the daylight saving differences between different countries.) )

                                                                                                                                                                                                                                                                       &nb

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.