JS Date: Get age by birth date

Source: Internet
Author: User

// JS date series: Get age of age by date of birth
// The strbirthday parameter is already a date string in the correct format such as 2007.02.09
// Add related JS methods for date processing, such as date judgment, to be added later
Function jsgetage (strbirthday)
{
VaR returnage;
VaR strbirthdayarr = strbirthday. Split (".");
VaR birthyear = strbirthdayarr [0];
VaR birthmonth = strbirthdayarr [1];
VaR Birthday = strbirthdayarr [2];

D = new date ();
VaR nowyear = D. getyear ();
VaR nowmonth = D. getmonth () + 1;
VaR nowday = D. getdate ();

If (nowyear = birthyear)
{
Returnage = 0; // The same year is 0 years old
}
Else
{
VaR agediff = nowyear-birthyear; // year difference
If (agediff> 0)
{
If (nowmonth = birthmonth)
{
VaR daydiff = nowday-birthday; // day difference
If (daydiff <0)
{
Returnage = agediff-1;
}
Else
{
Returnage = agediff;
}
}
Else
{
VaR monthdiff = nowmonth-birthmonth; // The difference between months
If (monthdiff <0)
{
Returnage = agediff-1;
}
Else
{
Returnage = agediff;
}
}
}
Else
{
Returnage =-1; // return-1 indicates that the input date of birth is later than today
}
}

Return returnage; // return age

}

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.