Jump to content

problem with date using java script


sandhya

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.