Lunarpages Web Hosting Forum

Scripting Languages Hosting Help => JavaScript Support => Topic started by: tcl on April 26, 2013, 02:56:33 AM

Title: Display current age using Javascript
Post by: tcl on April 26, 2013, 02:56:33 AM
Hi,
I am looking for a javascript to display the current age of an individual. The website is for my granddaughter and I would like to be able to say, "Today I am 6 months, 3 days old". Is there an easy way to do this??(http://Lunarpages.com/cat/6-5/1.gif)
Title: Re: Display current age using Javascript
Post by: scanman20 on April 26, 2013, 05:30:49 AM
Try this:

Code: [Select]
<p>Hi I'm
<script>
    function getAge(dateString) {
    var today = new Date();
    var birthDate = new Date(dateString);
    var age = today.getFullYear() - birthDate.getFullYear();
    var m = today.getMonth() - birthDate.getMonth();
    console.log(today, birthDate, age, m);
    if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
        age--;
    }
    return age + " years and " + m + " months old";
}
document.write(getAge('02-25-1973'));
</script>
 
Title: Display current age using Javascript
Post by: ronaldHobia on April 13, 2019, 04:04:20 AM
How to display current date using javascript in a textboxxhtml?

I have to create a text box in xhtml which displays the current date. The current date is produced by using javascript.
How do i do that? Do i create a function and call it? or do I do something else.
Thank You.