Calculate age with years, months, and days using jquery

In this article we take birth date and using the “GetDate” function to find the years, months and days. So let’s have a look at a practical example of how to calculate age on client side using Javascript.

Date Format: MM/DD/YYYY

Write down below function in head tag.

Code:

function getAge(dateString) {
var now = new Date();
var today = new Date(now.getYear(),now.getMonth(),now.getDate());

var yearNow = now.getYear();
var monthNow = now.getMonth();
var dateNow = now.getDate();

var dob = new Date(dateString.substring(6,10),
dateString.substring(0,2)-1,
dateString.substring(3,5)
);

var yearDob = dob.getYear();
var monthDob = dob.getMonth();
var dateDob = dob.getDate();
var age = {};
var ageString = “”;
var yearString = “”;
var monthString = “”;
var dayString = “”;
yearAge = yearNow – yearDob;

if (monthNow >= monthDob)
var monthAge = monthNow – monthDob;
else {
yearAge–;
var monthAge = 12 + monthNow -monthDob;
}

if (dateNow >= dateDob)
var dateAge = dateNow – dateDob;
else {
monthAge–;
var dateAge = 31 + dateNow – dateDob;

if (monthAge < 0) {
monthAge = 11;
yearAge–;
}
}

age = {
years: yearAge,
months: monthAge,
days: dateAge
};

if ( age.years > 1 ) yearString = ” years”;
else yearString = ” year”;
if ( age.months> 1 ) monthString = ” months”;
else monthString = ” month”;
if ( age.days > 1 ) dayString = ” days”;
else dayString = ” day”;

if ( (age.years > 0) && (age.months > 0) && (age.days > 0) )
ageString = age.years + yearString + “, ” + age.months + monthString + “, and ” + age.days + dayString + ” old.”;
else if ( (age.years == 0) && (age.months == 0) && (age.days > 0) )
ageString = “Only ” + age.days + dayString + ” old!”;
else if ( (age.years > 0) && (age.months == 0) && (age.days == 0) )
ageString = age.years + yearString + ” old. Happy Birthday!!”;
else if ( (age.years > 0) && (age.months > 0) && (age.days == 0) )
ageString = age.years + yearString + ” and ” + age.months + monthString + ” old.”;
else if ( (age.years == 0) && (age.months > 0) && (age.days > 0) )
ageString = age.months + monthString + ” and ” + age.days + dayString + ” old.”;
else if ( (age.years > 0) && (age.months == 0) && (age.days > 0) )
ageString = age.years + yearString + ” and ” + age.days + dayString + ” old.”;
else if ( (age.years == 0) && (age.months > 0) && (age.days == 0) )
ageString = age.months + monthString + ” old.”;
else ageString = “Oops! Could not calculate age!”;

return ageString;
}

Example:

alert(getAge(’04/06/1988′));

 

Happy Coding 🙂

Advertisements

Calculate age in SQL with years, months, and days

In this article we take birth date and using the “GetDate” function to find the years, months and days. So let’s have a look at a practical example of how to calculate age in SQL Server 2008. The example is developed in SQL Server 2008 using the SQL Server Management Studio.

Date Format = MM/DD/YYYY

DECLARE @date datetime, @tmpdate datetime, @years int, @months int, @days int

SELECT @date = ’04/06/1988′

SELECT @tmpdate = @date

SELECT @years = DATEDIFF(yy, @tmpdate, GETDATE()) – CASE WHEN (MONTH(@date) > MONTH(GETDATE())) OR (MONTH(@date) = MONTH(GETDATE()) AND DAY(@date) > DAY(GETDATE())) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(yy, @years, @tmpdate)
SELECT @months = DATEDIFF(m, @tmpdate, GETDATE()) – CASE WHEN DAY(@date) > DAY(GETDATE()) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(m, @months, @tmpdate)
SELECT @days = DATEDIFF(d, @tmpdate, GETDATE())

SELECT @years as Years, @months as Months, @days as Days

Output:

Output
calculate age in SQL with years, months, and days

Happy Coding 🙂

Expanding-Collapsing Sliding Div Jquery

To make own accordion using JQuery and HTML in just 4 steps.

Step: 1

All these expanding collapsing div code require following JQuery need to add in Script Tab,

add below line of code in your head tag.

<script src=’http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js&#8217; type=’text/javascript’></script>

Step: 2

Write below script in head tag,

<script type=”text/javascript”>

function toggleSlides() {
$(‘.toggle’).click(function (e) {
var id = $(this).attr(‘id’);
var widgetId = id.substring(id.indexOf(‘-‘) + 1, id.length);
$(‘#’ + widgetId).slideToggle();
$(this).toggleClass(‘sliderExpanded’);
$(‘.closeSlider’).click(function () {
$(this).parent().hide(‘slow’);
var relatedtoggle = ‘toggle-‘ + $(this).parent().attr(‘id’);
$(‘#’ + relatedtoggle).removeClass(‘sliderExpanded’);
});
});
};
$(function () {
toggleSlides();
});

</script>

Step: 3

<style type=”text/css”>

.slider
{
display: none;
}
.collapseSlider
{
display: none;
}
.sliderExpanded .collapseSlider
{
display: block;
}
.sliderExpanded .expandSlider
{
display: none;
}

</style>

Step:4 

<div>
<p class=”toggle” id=”toggle-slideOne”>
<span class=”expandSlider”>SHOW</span><span class=”collapseSlider”>HIDE</span>
</p>
<div class=”slider” id=”slideOne”>
<p>
Slide One lorem ipsum opsum…</p>
<span class=”closeSlider”>Close</span>
</div>
<p class=”toggle” id=”toggle-slideTwo”>
<span class=”expandSlider”>SHOW</span><span class=”collapseSlider”>HIDE</span>
</p>
<div class=”slider” id=”slideTwo”>
<p>
Slide Two lorem ipsum tupsum…</p>
<span class=”closeSlider”>Close</span>
</div>
</div>

Happy Coding 🙂