JS Date object and acquisition system current time detailed _javascript tips

Source: Internet
Author: User
Tags getdate numeric value

One, Date object
Every day an object, today to see the Date object, relative to time everyone must be very familiar with, should be in the site before many places will encounter such a time effect. Let's take a moment to recall the date object today.

Let's take a look at his definition first:
Dateobj = new Date ()
Dateobj = new Date (dateval)
Dateobj = new Date (year, month, date[, hours[, minutes[, Seconds[,ms]])

Required option. In the case of a numeric value, Dateval represents the number of milliseconds between the specified date and the global standard Time between midnight January 1, 1970. If it is a string, the dateval is parsed according to the rules in the parse method. The Dateval parameter can also be a vt_date value returned from some ActiveX® objects. Required option. Full year, for example, 1976 (instead of 76). Required option. Represents the month, which is an integer from 0 to 11 (January to December). Required option. Represents a date, which is an integer from 1 to 31. Options available. Represents milliseconds, an integer from 0 to 999.
The Date object holds a specific time period in milliseconds. If the value of a parameter is greater than its range or is negative, the other stored values are adjusted accordingly. For example, if you specify 150 seconds, JScript redefines the number to 2 minutes and 30 seconds.

If the number is NaN, the object does not represent a specific time period. If an argument is not passed to the Date object, it is initialized to the current time (UTC). You must assign a value to the object before it can be used.

Date objects are able to represent a range of dates approximately 285,616 years before and after January 1, 1970.

The Date object has two static methods that can be invoked without creating a Date object. They are parse and UTC.

The Date object has a lot of methods, and it's simply not listed here, Ok,see next!

Special attention:

**************************************************************

var mydate = new Date (2006,11,23);
Alert ("Year=" +mydate.getyear () + "******month=" +mydate.getmonth () + "********day=" +mydate.getdate ());

After the run, the result is year=2006 ******month=11********day=23

**************************************************************

This is all normal, and the problem arises when new date is passed in the month of 12.

**************************************************************

var mydate = new Date (2006,12,23);
Alert ("Year=" +mydate.getyear () + "******month=" +mydate.getmonth () + "********day=" +mydate.getdate ());

After the run, the result is year=2007 ******month=0********day=23

**************************************************************

A curious thing. Finally check the data that month this parameter's value is 0~11, cannot pass in 12 of this value. The solution is to put the month-1 when the value is passed, and then +1 when it is taken out of the way. I don't know who else has a better way.

Such as:

var date=new date (2010,0-1,15); 0 represents January, minus 1 becomes December.
Alert (date.getyear () + "," + (Date.getmonth () +1) + "," +date.getdate ());

var date=new date (2010,12-1,15);
Alert (date.getyear () + "," + (Date.getmonth () +1) + "," +date.getdate ());

parameter is a format for strings such as: 2010/1/6

Example:
var txtdeparturedate= ' 2010-1-6 '
Alert (Txtdeparturedate.replace (/-/g, '/')); Use regular to replace '-' all to/
var date = new Date (Txtdeparturedate.replace (/-/g, '/'));

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.