JS Built-in Object Date (1/2)

Source: Internet
Author: User

The date object should be a more commonly used object in a Web page effect, belonging to the native object (relative to the host object, i.e., host objects, for example, document).
The official version of this object is in the ecma-262 5th of the first 15.9, interested in the children's shoes can be seen.
This object provides a lot of practical methods, but there are some ways in which the implementations in each browser are different. Sink, Shan, often a small mistake, can cause fatal problems. For the robustness of the program, we should be aware of these differences in order to do a proactive, effective way to avoid these errors.
So, let's talk about a few common mistakes right now.

Compatibility issues with toLocaleString ()

According to the instructions in ecma-262 5th, this method should return the value of a string to display the date of the current time zone in the appropriate, readable manner. There is no specific provision to show what it looks like.
As a result, the difference arises.
See Example:
JScript Codedocument.write ((new Date ("1999/09/09")). toLocaleString ());

Results in each browser:
Firefox and ie:1999 year September 9 0:00:00
OPERA:1999/9/9 0:00:00
Safari:thursday, September 09, 1999 00:00:00
Chrome:thu Sep 1999 00:00:00 gmt+0800 (in-Standard time)
If, you always think, its return value is "September 9, 1999 0:00:00", and according to it to handle, then your program logic, certainly will have the problem. If it is the underlying method of comparison, it may affect large functionality.

For the display of dates, you can use your own methods to eliminate this discrepancy. The following program is not robust, it means so. JScript Code
/**
* Date:date Object instance
*/

function Localedate (date) {
        return date.getfullyear () + year "
                         + (Date.getmonth () + 1) + "month"
                         + date.getdate ( ) + "Day" + ""
                & nbsp;       + date.gethours () + ":"
                         + Date.getminutes () + ":"
                         + date.getseconds ();
}

There are two points to be aware of:
1. When the year was obtained, The getFullYear () was used, without the use of getyear ();
2. Month use GetMonth (), need to add 1 is really the month value.

Why not use getyear () and use getFullYear ()?
Because GetYear () also has compatibility issues.

getyear () Compatibility issues

MSDN describes this method in this way:
For 1900-1999, the call to the GetYear () method returns a two-digit integer that represents the gap between the saved year and the 1900 year period. For other years, the return value is a four-bit integer.
See msdn:http://msdn.microsoft.com/en-us/library/x0a9sc10%28vs.85%29.asp Tutorial X
Note that the above "other" refers to 1900 ago, 2000 and beyond.

Other browsers in this section of the official documentation indicate:
The value returned by getyear () is always the difference between the year in the Date object and the 1900 year.
This method is outdated and is provided to maintain backward compatibility. Use the getFullYear method instead.

Home 1 2 last page

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.