Jump to content

Recommended Posts

Hi all,

    I"ve a form in which i "ve to validate using age. i"ve a text box called 'DateOfBirth' which inputs date of birth using a pop up calender. Using that i"ve to calculate the age of the user on change.

    For this i'm using a java script function onChange event for the field 'DateOfBirth'. In that function i"ve taken the Date() to take present date. That function displays the date as ddd mmm dd yyyy HH:MM:ss format like Fri Jan 09 2009 14:55:21. Here i should get the date in dd mm yyyy format. How can I get it. And how can I calculate the age. Can anyone help me.

Link to comment
https://forums.phpfreaks.com/topic/140129-problem-with-date-using-java-script/
Share on other sites

the Date() function returns a date object you can  use the object's methods to get the specific values

example

var today=Date();
//the day
var day=today.getDay();

//the month
var month=today.getMonth();

//the year
var year=today.getFullYear();

function getAge(bDateStr)
{
    var today  = new Date();
    var tDay   = today.getDate();
    var tMonth = today.getMonth();
    var tYear  = today.getFullYear();

    var bDateAry = bDateStr.split('-');
    var bDay     = bDateAry[0];
    var bMonth   = bDateAry[1]-1;
    var bYear    = bDateAry[2];

    var age = (tYear - bYear);

    if (bMonth>tMonth || (bMonth==tMonth && bDay>tDay))
    {
        var age = tYear - bYear - 1;
    }

    return age;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.