求一年龄的算法
我做的是c#应用程序,有出生年月字段,客户让这个字段为字符型,现在给任一个时间如2007年6月6日,怎么求年龄段呢,如31-35。
------解决方案--------------------31-35?是怎么算出来的?
------解决方案--------------------PjLoginInfo loginInfo = PjLoginInfoUtil.GetLoginInfo();
public string Getage(M_staffVO vo, IDBContext context)
{
//SQL文を作成する。
StringBuilder builder = new StringBuilder();
builder.Append( " SELECT ");
builder.Append( " DATEDIFF(yy,SEIYMD,getdate()) AS AGE ");
builder.Append( " FROM ");
builder.Append( " M_STAFF ");
builder.Append( " WHERE 0=0 ");
builder.Append( " AND KAISHACD = ' " + loginInfo.Kaishacd + " ' ");
builder.Append( " AND STAFFCD = ' " + vo.Staffcd + " ' ");
builder.Append( " AND DELFLG = '0 ' ");
//SqlStatementを生成する。
SqlStatement sqlStatement = new SqlStatement(builder.ToString(), CommandType.Text);
String sqlStr = sqlStatement.ToString();
//検索結果を取得する。
IDataReader dataReader = DBConnector.ExecuteQuery(sqlStatement, context);
string age = " ";
if (dataReader.Read())
{
age = Convert.ToString(dataReader[ "AGE "]);
}
dataReader.Close();
return age;
}
------解决方案--------------------你的问题没有说明白阿!!
------解决方案--------------------根据出生日期和当前日期就可以计算年龄了
------解决方案--------------------string st = "2007-6-6 ";
DateTime dt;
DateTime.TryParse(st, out dt);
DateTime dt1 = new DateTime(dt.Year - 31, dt.Month, dt.Day);
是否这个意思?
------解决方案--------------------南方算虚岁,北方算实岁,呵呵,也没那么简单吧?
------解决方案--------------------
------解决方案--------------------1,截取年字段出来和当前日期年字段相减得到年数
2,DateTime d1;
DateTime d2;
//赋值
TimeSpan t = d2-d1;
int days = (int)t.TotalDays;
int year = days/365;
------解决方案--------------------set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
create FUNCTION [dbo].[AGE] (@startdate datetime,@enddate datetime)
RETURNS int
WITH EXECUTE AS CALLER
AS
BEGIN
DECLARE @addday int
declare @year int
declare @age int
SET @addday=0
set @year=datepart(year,@startdate)
while (@year <=datepart(year,@enddate))
begin
if (@year=datepart(year,@enddate) and datepart(month,@enddate)> =3)
begin
if ((@year%4=0 and @year%100 <> 0) or (@year%400=0))
set @addday=@addday+1
end
else
if (((@year%4=0 and @year%100 <> 0) or (@year%400=0)) and @year <> datepart(year,@enddate))
set @addday=@addday+1
set @year=@year+1
end
set @age=(datediff(day,@startdate,@enddate)-@addday)/365
RETURN(@age)
END
------解决方案--------------------