public class Person
{
public String Name;
public DateTime birthday;
public Int32 Age;
}
private void button1_Click(object sender, EventArgs e)
{
database db = new database();
string sql = "select * from JZK_pat where times=1";
SqlDataReader mydr;
db.runsql(sql, out mydr);
while (mydr.Read())
{
Person item = new Person();
int index = mydr.GetOrdinal("birthday");
item.birthday = mydr.IsDBNull(index) ? new DateTime("1900-01-01") : mydr.GetDateTime(index);
item.Age = DateTime.Now.Year - item.birthday.Year;
listBox1.Items.Add(item);
}
mydr.Close();
}
sql数据库中有多条数据,字段名包括(姓名,性别,出生日期)
想用winform程序的textbox.Item显示出所有数据,但显示的格式为每一行(姓名,性别,年龄)
自己做的是SqlDataReader,但在while循环里无法实现多条出生日期转换成年龄
我是新手,请大神们指导一下,谢谢
字符串用 DateTime.Parse转,而不是new DateTime,ide会有参数类型提示,除非你用记事本来编写代码
public int GetAgeByBirthdate(DateTime birthdate)
{
DateTime now = DateTime.Now;
int age = now.Year - birthdate.Year;
if (now.Month < birthdate.Month || (now.Month == birthdate.Month && now.Day < birthdate.Day))
{
age--;
}
return age < 0 ? 0 : age;
}
public class Person
{
public String Name;
public DateTime birthday;
public Int32 Age;
}
private void button1_Click(object sender, EventArgs e)
{
database db = new database();
string sql = "select * from JZK_pat where times=1";
SqlDataReader mydr;
db.runsql(sql, out mydr);
while (mydr.Read())
{
Person item = new Person();
int index = mydr.GetOrdinal("birthday");
item.birthday = mydr.IsDBNull(index) ? DateTime.Parse("1900-01-01") : mydr.GetDateTime(index);
item.Age =GetAgeByBirthdate(item.birthday);
listBox1.Items.Add(item);
}
mydr.Close();
}
实在是抱歉...DateTime不是那样定义的... 可是楼下两位直接复制粘贴 也太没品了吧
直接在sql语句中处理,将日期转换成年龄 SELECT DATEDIFF(YEAR,'1990-01-01',GETDATE());
你实体类再加一个字段 是年龄,然后取数据的时候,减一下现在的时间 不就好了吗
public class Person
{
public String Name;
public DateTime birthday;
public Int32 Age;
}
public List<Person> SelectPurchaseContractLog(String PurchasingMan)
{
List<Person> list = new List<Person>();
String sql = @"......";
command.CommandText = sql;
command.Parameters.Add("purchasing_man", SqlDbType.NChar, 255).Value = PurchasingMan;
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Person item = new Person();
int index = reader.GetOrdinal("name");
item.Name = reader.IsDBNull(index) ? String.Empty : reader.GetString(index);
index = reader.GetOrdinal("birthday");
item.birthday= reader.IsDBNull(index) ? new DateTime(1900,1,1) : reader.GetDateTime(index);
item.Age= DateTime.Now.Year - item.birthday.Year;
list.Add(item);
}
}
return list;
}
算实岁
select
cast((DATEDIFF(day,出生日期,getdate())+DATEDIFF(day,出生日期,getdate())/365*0.25)/365
as int)
算虚岁
select
celling((DATEDIFF(day,出生日期,getdate())+DATEDIFF(day,出生日期,getdate())/365*0.25)/365)
public class Person
{
public String Name;
public DateTime birthday;
public Int32 Age;
}
private void button1_Click(object sender, EventArgs e)
{
database db = new database();
string sql = "select * from JZK_pat where times=1";
SqlDataReader mydr;
db.runsql(sql, out mydr);
while (mydr.Read())
{
Person item = new Person();
int index = mydr.GetOrdinal("birthday");
item.birthday = mydr.IsDBNull(index) ? new DateTime("1900-01-01") : mydr.GetDateTime(index);
item.Age = DateTime.Now.Year - item.birthday.Year;
listBox1.Items.Add(item);
}
mydr.Close();
}
("1900-01-01")下面显示红色波浪线是为什么
public class Person
{
public String Name;
public DateTime birthday;
public Int32 Age;
}
private void button1_Click(object sender, EventArgs e)
{
database db = new database();
string sql = "select * from JZK_pat where times=1";
SqlDataReader mydr;
db.runsql(sql, out mydr);
while (mydr.Read())
{
Person item = new Person();
int index = mydr.GetOrdinal("birthday");
item.birthday = mydr.IsDBNull(index) ? new DateTime("1900-01-01") : mydr.GetDateTime(index);
item.Age = DateTime.Now.Year - item.birthday.Year;
listBox1.Items.Add(item);
}
mydr.Close();
}
//代码中("1900-01-01")下面显示为波浪线,不知道是什么原因,请大神指教一下,谢谢
代码中("1900-01-01")下面显示为红色波浪线,不知道是什么原因,请大神指教一下,谢谢
DateTime birthday = new DateTime(2012, 12,12);
DateTime now = DateTime.Now;
if(birthday > now)
{
Console.WriteLine("出生日期要小于当前日期");
return;
}
int year = now.Year - birthday.Year;
int month = now.Month - birthday.Month;
if(month < 0)
{
year--;
month += 12;
}
int day = now.Day - birthday.Day;
if(day < 0)
{
month--;
if(month < 0)
{
year--;
month = 11;
}
day += DateTime.DaysInMonth(now.Year, now.Month - 1);
}
Console.WriteLine("{0}年{1}月{2}天", year, month, day);
namespace ExDevilLee.DateTime
{
/// <summary>
/// 年龄计算工具类
/// 由两个日期参数计算出精确年龄字符串
/// </summary>
public static class CalculateAgeCls
{
/// <summary>
/// 获得年龄字符串:某个日期点到今天的年龄
/// 默认返回:xx岁xx月xx天
/// </summary>
/// <param name="p_FirstDateTime">第1个日期参数</param>
public static string GetAgeString(System.DateTime p_FirstDateTime)
{
return CalculateAgeString(p_FirstDateTime, System.DateTime.Now, null);
}
/// <summary>
/// 获得年龄字符串:某个日期点到今天的年龄
/// 默认返回:xx岁xx月xx天
/// </summary>
/// <param name="p_FirstDateTime">第1个日期参数</param>
/// <param name="p_Format">返回字符串的格式,默认为:{0}岁{1}月{2}天</param>
public static string GetAgeString(System.DateTime p_FirstDateTime, string p_ReturnFormat)
{
return CalculateAgeString(p_FirstDateTime, System.DateTime.Now, p_ReturnFormat);
}
/// <summary>
/// 获得年龄字符串:两个日期点之间的年龄
/// 默认返回:xx岁xx月xx天
/// </summary>
/// <param name="p_FirstDateTime">第1个日期参数</param>
/// <param name="p_SecondDateTime">第2个日期参数</param>
public static string GetAgeString(System.DateTime p_FirstDateTime, System.DateTime p_SecondDateTime)
{
return CalculateAgeString(p_FirstDateTime, p_SecondDateTime, null);
}
/// <summary>
/// 获得年龄字符串:两个日期点之间的年龄
/// 默认返回:xx岁xx月xx天
/// </summary>
/// <param name="p_FirstDateTime">第1个日期参数</param>
/// <param name="p_SecondDateTime">第2个日期参数</param>
/// <param name="p_Format">返回字符串的格式,默认为:{0}岁{1}月{2}天</param>
public static string GetAgeString(System.DateTime p_FirstDateTime, System.DateTime p_SecondDateTime, string p_ReturnFormat)
{
return CalculateAgeString(p_FirstDateTime, p_SecondDateTime, p_ReturnFormat);
}
/// <summary>
/// 计算年龄字符串
/// 默认返回:xx岁xx月xx天
/// </summary>
/// <param name="p_FirstDateTime">第1个日期参数</param>
/// <param name="p_SecondDateTime">第2个日期参数</param>
/// <param name="p_Format">返回字符串的格式,默认为:{0}岁{1}月{2}天</param>
private static string CalculateAgeString(System.DateTime p_FirstDateTime, System.DateTime p_SecondDateTime, string p_ReturnFormat)
{
//判断时间段是否为正。若为负,调换两个时间点的位置。
if (System.DateTime.Compare(p_FirstDateTime, p_SecondDateTime) > 0)
{
System.DateTime stmpDateTime = p_FirstDateTime;
p_FirstDateTime = p_SecondDateTime;
p_SecondDateTime = stmpDateTime;
}
//判断返回字符串的格式。若为空,则给默认值:{0}岁{1}月{2}天
if (string.IsNullOrEmpty(p_ReturnFormat)) p_ReturnFormat = "{0}岁{1}月{2}天";
//定义:年、月、日
int year, month, day;
//计算:天
day = p_SecondDateTime.Day - p_FirstDateTime.Day;
if (day < 0)
{
day += System.DateTime.DaysInMonth(p_FirstDateTime.Year, p_FirstDateTime.Month);
p_FirstDateTime = p_FirstDateTime.AddMonths(1);
}
//计算:月
month = p_SecondDateTime.Month - p_FirstDateTime.Month;
if (month < 0)
{
month += 12;
p_FirstDateTime = p_FirstDateTime.AddYears(1);
}
//计算:年
year = p_SecondDateTime.Year - p_FirstDateTime.Year;
//返回格式化后的结果
return string.Format(p_ReturnFormat, year, month, day);
}
}
}
1、已知一个日期点,求该日期到当前日期(今天)的精确年龄;
2、已知两个日期点,求这两个日期点的精确年龄;