在C#3.0中,我正在执行下面的代码来声明一个DateTime属性和一个只读的int age属性.
public class MyClass{
public DateTime dateOfBirth{ get; set; }
public int age { get; }
public MyClass(){}
public int CalculateAge(){}
}
Run Code Online (Sandbox Code Playgroud)
但是,当某人以某种形式输入他的出生日期时,我怎样才能获得这个更新的年龄(只读它)?
我的应用程序需要将客户当前年龄调整+0.5(如果已经过去6个月).
代码应该看起来像这样,但是6个月内会有多少个滴答?
if (DateTime.Today - dateOfBirth.Date > new TimeSpan(6))
{
adjust = 0.5M;
}
else
{
adjust = 0M;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢
使用Flex计算年龄的最佳方法是什么?
可能重复:
如何计算C#中某人的年龄?
我正在尝试创建一个表单,其中输入了客户的出生日期,并且他的年龄会自动显示在txtAge中:
private void Form3_Load(object sender, EventArgs e)
{
dtpDob.Value = DateTime.Today.Date;
SqlConnection conn = new SqlConnection();
}
private void dtpDOB_Leave(object sender, System.EventArgs e)
{
System.DateTime dob = default(System.DateTime);
dob = dtpDob.Value.Date;
txtAge.Text = DateTime.DateDiff(DateInterval.Year, dob, DateTime.Today.Date);
}
Run Code Online (Sandbox Code Playgroud)
但我得到这些错误:
'System.DateTime'不包含'DateDiff'的定义.
当前上下文中不存在名称"DateInterval".
我想通过计算当前年龄来计算出生年份,例如他的年龄是30岁,在datetimepicker框中,日期应该显示为01/01/1982.它应该是自动计算.
int todayyears = int.Parse (DateTime.Now.Year.ToString());
int a = int.Parse (int.Parse(txt_age.Text).ToString());
string dif = (todayyears - a).ToString();
txt_dob.Text = dif;
Run Code Online (Sandbox Code Playgroud)
这txt_age是给定的输入值,txt_dob在给出输入后自动显示出生日期txt_age.
我看到这已被问过很多次了.很多人都回答了,但我还没能让它发挥作用.
这是我从搜索中得到的代码:
public static int CalculateAge(DateTime birthDate)
{
DateTime now = DateTime.Today;
int years = now.Year - birthDate.Year;
if (now.Month < birthDate.Month || (now.Month == birthDate.Month && now.Day < birthDate.Day))
--years;
return years;
}
Run Code Online (Sandbox Code Playgroud)
如何从TextBox提供出生日期?
我使用Jquery日历以dd-mm-yy格式将日期提供给TextBox3 ..
现在我想从提供的日期计算年龄,然后点击按钮保存在数据库中..
我得到了DB部分的保存,但是如何将TextBox值插入上面的代码中,然后使用years将其保存在我的按钮点击事件上?
我有一个叫做Person属性的类Name,DateOfBirth和Age.Name并且DateOfBirth在创建类实例时提供但我希望在创建类的实例时Age自动计算属性.我知道如何使用Parametrized Constructor来做到这一点,但是当我试图理解Properties的功能时,我试图遵循程序,但它没有给出预期的输出.
using System;
namespace Property
{
class Person
{
#region private varaibles
private int _age;
#endregion
#region properties
public string Name { get; set; }
public DateTime DateOfBirth { get; set; }
public int Age
{
get { return _age; }
set { _age = (DateTime.Now - DateOfBirth).Days / 365; }
}
#endregion properties
}
class Program
{
static void Main(string[] args)
{ …Run Code Online (Sandbox Code Playgroud) 如何准确地DateTime在"年"中获得两个对象之间的差异(以年为单位)?
DateTime.Subtract()给出差异,TimeSpan最大面额是天.
那么,如果我想要准确地说,今天和1988年的一天(比如1988年3月29日)之间的差异,是否有一种"更容易"的方式来获得这个人的准确年龄?
我试过的是:
DateTime March291988 = DateTime.Parse("29/03/1988");
TimeSpan ts = DateTime.Now.Subtract(March291988);
int years = (ts.Days/365);
Run Code Online (Sandbox Code Playgroud)
更重要的是,问题是:如何从TimeSpan转换为DateTime.
我将有两个约会.出生日期和事件日期
我想计算出生日期和活动日期之间的时间是否小于或大于18年.活动日期将始终大于出生日期.
如果低于18,则其他为假.
EG:DOB 29/02/1976.活动日期2017年12月29日
这里的人超过18岁!
我不确定使用DateAdd或DateDiff是最好的方法 - 或者即使使用DateTime.Days && DateTime.Months等
c# ×10
datetime ×4
.net ×2
asp.net ×2
actionscript ×1
apache-flex ×1
c#-3.0 ×1
datediff ×1
properties ×1
winforms ×1