Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi;
I used following code to calculate age in year ,month and day.but i got age in sql and server side different value .

if current date='30/10/2014'
and date of birth='31/12/2011'

i got value in server side -Age:2 Year(s)9 Month(s)29 Day(s)
and in sql -Age:2 Year(s)9 Month(s)30 Day(s)

if month 31 days then claculation different in both sql and c#

*******************************************************
DateTime dt = DateTime.Now;
DateTime dob = Convert.ToDateTime(txtDobE.Text);

int days = dt.Day - dob.Day;
if (days < 0)
{
dt = dt.AddMonths(-1);
days += DateTime.DaysInMonth(dt.Year, dt.Month);
}

int months = dt.Month - dob.Month;
if (months < 0)
{
dt = dt.AddYears(-1);
months += 12;
}

int years = dt.Year - dob.Year;

txtAge.Text = String.Format("Age: {0} Year(s) {1} Month(s) {2} Day(s) ",
Years, Months, Days);

***********************

and in sql
**********************
CREATE function [dbo].[Age](@dayOfBirth datetime, @today datetime)
RETURNS varchar(100)
AS

Begin
DECLARE @tmpdate datetime
DECLARE @years int, @months int, @days int
declare @Age varchar(max)=''
SELECT @tmpdate = @dayOfBirth
SELECT @years = DATEDIFF(yy, @tmpdate, GETDATE()) - CASE WHEN (MONTH(@dayOfBirth) > MONTH(GETDATE())) OR (MONTH(@dayOfBirth) = MONTH(GETDATE()) AND DAY(@dayOfBirth) > DAY(GETDATE())) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(yy, @years, @tmpdate)
SELECT @months = DATEDIFF(m, @tmpdate, GETDATE()) - CASE WHEN DAY(@dayOfBirth) > DAY(GETDATE()) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(m, @months, @tmpdate)
SELECT @days = DATEDIFF(d, @tmpdate, GETDATE())

--set @thisYearBirthDay = @dayOfBirth--DATEADD(year, DATEDIFF(year, @dayOfBirth, @today), @dayOfBirth)
--set @years = DATEDIFF(year, @dayOfBirth, @today) - (CASE WHEN @thisYearBirthDay > @today THEN 1 ELSE 0 END)
--set @months = MONTH(@today - @thisYearBirthDay) - 1
--set @days = DAY(@today - @thisYearBirthDay) - 1
if(@years>0)
BEGIN
set @Age=@Age+cast(@years as varchar(2)) + ' years '
END
if(@months>0)
BEGIN
set @Age=@Age+cast(@months as varchar(2)) + ' months '
END
if(@days>0)
BEGIN
set @Age=@Age+cast(@days as varchar(2)) + ' days '
END

return @Age
Posted

Hi,


I think you are doing bit more, its very simple

In server Side :

useDateTime.Subtract[^]

System.DateTime date2 = new System.DateTime(1996, 12, 6, 13, 2, 0);
			System.DateTime date3 = new System.DateTime(1996, 10, 12, 8, 42, 0);

			// diff1 gets 185 days, 14 hours, and 47 minutes.
			System.TimeSpan diff1 = date2.Subtract(date1);


Use TimeSpan[^] and calculate exact age.

in SQL Server :
Use DateDiff[^]

SQL
DECLARE @date datetime, @tmpdate datetime, @years int, @months int, @days int
SELECT @date = '2/29/04'

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, @months, @days


Ref : http://stackoverflow.com/questions/57599/how-to-calculate-age-in-t-sql-with-years-months-and-days[^]
 
Share this answer
 
Hi,

This is a very good link that will help you calculate the age even if it is a lear or not.
Follow here.

Thanks
Sisir Patro
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900