Answered by:
Get Age From Given Date

Question
-
User-807418713 posted
Hello
In My Textbox1 In which date format is like this for example
15-Apr-1985
I want to show age in Textbox2
Monday, April 15, 2019 6:24 PM
Answers
-
User-893317190 posted
Hi Gopi.MCA ,
You could also get age using c#.
Below is my code.
DateTime time; int age = 0; if (DateTime.TryParseExact("15-Apr-1985", "dd-MMM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out time)) { while ((time= time.AddYears(1)) < DateTime.Now) { age++; } } Response.Write(age);
The result.
For more information , please refer to https://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 16, 2019 2:11 AM
All replies
-
User475983607 posted
Hello
In My Textbox1 In which date format is like this for example
15-Apr-1985
I want to show age in Textbox2
Very Simple. Return the age using the same query you are using to get the birth date.
USE [AdventureWorks] GO SELECT [ModifiedDate], DATEDIFF(year,[ModifiedDate], GETDATE()) AS [Age] FROM [Person].[Person] GO
Monday, April 15, 2019 7:06 PM -
User2053451246 posted
This function will return the correct age:
CREATE FUNCTION [dbo].[fn_GetAge] ( @DOB AS DATETIME , @AsOfDate AS DATETIME ) RETURNS INT AS BEGIN DECLARE @age INT IF CAST(DATEPART(M, @AsOfDate) AS INT) > CAST(DATEPART(m, @DOB) AS INT) SET @age = CAST(DATEDIFF(YYYY, @DOB, @AsOfDate) AS INT) ELSE IF CAST(DATEPART(M, @AsOfDate) AS INT) = CAST(DATEPART(m, @DOB) AS INT) IF DATEPART(D, @AsOfDate) >= DATEPART(d, @DOB) SET @age = CAST(DATEDIFF(YYYY, @DOB, @AsOfDate) AS INT) ELSE SET @age = CAST(DATEDIFF(YYYY, @DOB, @AsOfDate) AS INT) -1 ELSE SET @age = CAST(DATEDIFF(YYYY, @DOB, @AsOfDate) AS INT) - 1 RETURN @age END GO
Usage:
SELECT
dbo.fn_GetAge(DOB, GETDATE())
FROM TableName
Monday, April 15, 2019 7:31 PM -
User-893317190 posted
Hi Gopi.MCA ,
You could also get age using c#.
Below is my code.
DateTime time; int age = 0; if (DateTime.TryParseExact("15-Apr-1985", "dd-MMM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out time)) { while ((time= time.AddYears(1)) < DateTime.Now) { age++; } } Response.Write(age);
The result.
For more information , please refer to https://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, April 16, 2019 2:11 AM