The difference between getyear () and getFullYear () in JavaScript

Source: Internet
Author: User
Tags getdate

The practice of getting the current year in JS is

The code is as follows Copy Code
var object = new Date (), Object.getyear ()

, to get the year, so that there will be browser compatibility issues, in IE can get the desired results, but in the FF line, the results in FF and the current desired year difference of 1900, you can use the following approach to avoid this problem:

The code is as follows Copy Code

var object = new Date ();
var mydate = (Object.getyear () <1900)? (1900+object.getyear ()): Object.getyear ();
Document.wite (mydate);

But now you can use getFullYear to avoid the compatibility of IE and FF, getfullyear () in IE and FF can get the results we want, so as to avoid the browser compatibility problem.

Instance

The code is as follows Copy Code

<title> Day three Drop-down frame linkage </title>
<meta http-equiv= ' content-type ' content= ' text/html; charset=gb2312 ' >
<meta name= ' author ' content= ' Oiea ' >
  
<script language= "JavaScript" >
<!--
function Initdate (year,month,day)
{
The initial number of days per month
Mondays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
The current year
var y = new Date (). getFullYear ();
The current month
var m = new Date (). getmonth () +1; JavaScript Month is 0-11
But before the day
var d = new Date (). GetDate ();

This year, 2 years back, fill year dropdown box
for (var i = y; I < (y+2); i++)
{
Year.options.add (New Option (i,i));
}
Select this year
Year.value=y;

Fill Month Drop-down Box
for (var i = 1; I <= i++)
{
Month.options.add (New Option (i,i));
}
Select Month
Month.value = m;

Get initialization days for the month
var n = mondays[m-1];
If February, the number of days plus 1
if (m = = 2 && isleapyear (Year.options[year.selectedindex].value))
n++;
Fill Date drop down box
Createday (N,day);
Selected day
Day.value = new Date (). GetDate ();
}

    function Change (year,month,day)//month changes, change day
    {
   &nbs p;     var y = year.options[year.selectedindex].value;
         var m = month.options[month.selectedindex].value;
        //if (m = = "") {  clearoptions (day); return;}
         var n = mondays[m-1];
         if (M ==2 && isleapyear (y))
    &NBSP;&NB sp;   {
             n++;
         }
         createday (n,day)
  & nbsp }


function Createday (n,day)//Fill Date dropdown box
{
Clear the dropdown box
Clearoptions (day);
A few days, just write a few
for (var i=1; i<=n; i++)
{
Day.options.add (New Option (i,i));
}
}

function Clearoptions (CTL)//Delete all options in the Drop-down box
{
for (var i=ctl.options.length-1; i>=0; i--)
{
Ctl.remove (i);
}
}

function Isleapyear (year)//judge whether leap year
{
Return (Year%4==0 | | (year%100 ==0 && year%400 = 0));
}
-->
</script>
<body onload= "Initdate (document.form1.select_year,document.form1.select_month,document.form1.select_day)" >
<form name= "Form1" >
<select name= "Select_year" onchange= "Change (this, Document.form1.select_month, Document.form1.select_day)" > </select>year
<select name= "Select_month" onchange= "Change (document.form1.select_year, This,document.form1.select_day)" > </select>month
<select name= "Select_day" ></select>day
</form>
</body>

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.