When using JS to display the date time, the display of the year in Chrome is incorrect (as shown below), but is normal in IE. For example, the current year is 2014, and the number is displayed at 114.
It seems that the Chrome browser and IE browser's implementation of the GetYear function is different. Modifying it to getFullYear can solve the problem.
In fact, JS gets the current time of the year there are three common functions: getyear (), getFullYear (),
getUTCFullYear () These three are all the method functions of the Date object in JS to get the current year.
Let's take a look at the three functions below:
The GetYear () method was born earlier and always used OK in the early days, but after 2000 years this method was problematic because in browsers like Firefox and Safari, getyear always returns the difference between the year and 1900 years, like returning 98 in 1998, And 2009 will show 109, if everyone is so good, to add together, Microsoft's own in IE browser to the getyear to amend, but Firefox (the latest version does not fix this problem) still in the dark, still honest according to the original rules to resolve getyear, Could have expected the user to amend themselves, so that there is no use, so getFullYear, getUTCFullYear was born.
1, getyear () function
Use the GetYear () method to return a two-bit or four-digit year, and the number returned with getyear () is not necessarily 4-bit! The GetYear () method, which is between 1900 and 1999, returns only two digits. The previous or subsequent years have returned four digits, such as 2009, the JavaScript parser should have returned 2009, while the browser calculates the return of 109. This should be the early agreement, and IE immersed in the change. This function has been deprecated and is not recommended for use.
The code is as follows |
Copy Code |
<script type= "Text/javascript" > var d = new Date (); document.write (D.getyear ());//ie output 2009,firefox Output 109 </script> |
2, Getfullyea () function
The getFullYear function does not have this problem. getFullYear () method can return a four-digit year, so that everyone (ie and Firefox, etc.) do not need to operate, directly to the output of the analytic value.
The code is as follows |
Copy Code |
<script type= "Text/javascript" > var d = new Date (); document.write (D.getfullyear ());//ie Output 2009,firefox Output 2009 </script> |
3, getUTCFullYear () function
The getUTCFullYear () function returns a four-digit number to represent the year based on the UTC time. The getFullYear () method theory is completely different, although the output is the same for most of the time, but if the day date is December 31 or January 1, the return value of getUTCFullYear () and getFullYear () may be different. Depending on the relationship between the local time zone and the UTC universal duration, the difference.
The code is as follows |
Copy Code |
<script type= "Text/javascript" > var d = new Date (); document.write (D.getutcfullyear ());//ie Output 2009,firefox Output 2009 </script> |
Complete JS Code
The code is as follows |
Copy Code |
function Getcurdate () { var d = new Date (); VAR week; Switch (D.getday ()) { Case 1:week= "Monday"; Break Case 2:week= "Tuesday"; Break Case 3:week= "Wednesday"; Break Case 4:week= "Thursday"; Break Case 5:week= "Friday"; Break Case 6:week= "Saturday"; Break default:week= "Sunday"; } var years = D.getfullyear (); var month = Add_zero (D.getmonth () +1); var days = Add_zero (D.getdate ()); var hours = Add_zero (D.gethours ()); var minutes = Add_zero (D.getminutes ()); var Seconds=add_zero (D.getseconds ()); var ndate = years+ "year" +month+ "month" +days+ "Day" +hours+ ":" +minutes+ ":" +seconds+ "" +week "; var Divt=document.getelementbyid ("divt"); Divt.innerhtml= ndate; }
function Add_zero (temp) { if (temp<10) return "0" +TEMP; else return temp; }
SetInterval ("Getcurdate ()", 100); </script> Html <div class= "box01" id= "DIVT" > </div> |
The effect is as follows