问题描述
嗨;
我使用下面的代码来计算年,月和日的年龄。但我在sql和服务器端的年龄不同。
如果当前日期= '30 / 10/2014'
和出生日期= '31 / 12/2011'
i在服务器端获得价值 - 年龄:2年9个月29天
和sql -Age:2年9个月30一天(s)
如果是31天,那么sql和c中的claculation会有所不同#
** ************************************************** ***
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 mon ths = dt.Month - dob.Month;
if(months< 0)
{
dt = dt.AddYears(-1);
个月+ = 12;
}
int years = dt.Year - dob.Year;
txtAge.Text = String。格式(年龄:{0}年{1}个月{2}天,
年,月,日);
***********************
in sql
**********************
CREATE函数[dbo]。[年龄](@ dayOfBirth datetime,@ dayodaytime)
RETURNS varchar(100)
AS
开始
DECLARE @tmpdate datetime
DECLARE @years int,@ months int,@ days int
声明@Age varchar(max)=''
SELECT @tmpdate = @dayOfBirth
SELECT @years = DATEDIFF(yy,@ ttdate,GETDATE()) - CASE WHEN(月(@dayOfBirth)> MONTH(GETDATE()) )或(月(@dayOfBirth) = MONTH(GETDATE())和DAY(@dayOfBirth)> DAY(GETDATE()))那么1 0结束
SELECT @tmpdate = DATEADD(yy,@ year,@ tmpdate)
SELECT @months = DATEDIFF(m, @tmpdate,GETDATE()) - 当天的情况(@dayOfBirth)> DAY(GETDATE())那么1结束0结束
SELECT @tmpdate = DATEADD(m,@ months,@ tmpdate)
SELECT @days = DATEDIFF(d,@ tmpdate,GETDATE())
--set @thisYearBirthDay = @ dayOfBirth - DATEADD(年,DATEDIFF(年,@ dayOfBirth,@今天),@ dayOfBirth)
- 设置@years = DATEDIFF(年,@ dayOfBirth,@今天) - (情况@thisYearBirthDay> @today那么1节0结束)
- 设置@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))+ '月'
结束
if(@ days> 0)
BEGIN
set @ Age = @ Age + cast(@days as varchar(2))+'days'
END
返回@Age
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
推荐答案
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[^]
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
参考: []
这篇关于asp.net中的年龄计算c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!