C# 基于C语言中出生日期的简单年龄计算器#
我创建了一个简单的年龄计算器。我想删除小数位,但问题是当我将C# 基于C语言中出生日期的简单年龄计算器#,c#,datetime,decimal,calculator,C#,Datetime,Decimal,Calculator,我创建了一个简单的年龄计算器。我想删除小数位,但问题是当我将now减为bday时 示例:我输入了2012、10和23,现在的日期是2014-10-22, 因此,当2014.1022-2012.1023时,结果将是1.9999…我想删除所有小数位,保留整数1,但我使用String.Format(“{0:00}”它将结果四舍五入到02即使我使用ConvertToInt32,我也不想使用它,因为它需要很多代码 有什么想法吗 static void Main(string[] args)
now
减为bday
时
示例:我输入了2012
、10
和23
,现在的日期是2014-10-22
,
因此,当2014.1022-2012.1023
时,结果将是1.9999…
我想删除所有小数位,保留整数1
,但我使用String.Format(“{0:00}”
它将结果四舍五入到02
即使我使用ConvertToInt32
,我也不想使用它,因为它需要很多代码
有什么想法吗
static void Main(string[] args)
{
string year, month, day = string.Empty;
Console.WriteLine("Enter your Birthdate:");
Console.WriteLine("Year :");
year = Console.ReadLine();
Console.WriteLine("Month :");
month = Console.ReadLine();
Console.WriteLine("Day :" );
day = Console.ReadLine();
try
{
DateTime date = Convert.ToDateTime(year + "-" + month + "-" + day);
var bday = float.Parse(date.ToString("yyyy.MMdd"));
var now = float.Parse(DateTime.Now.ToString("yyyy.MMdd"));
if (now < bday)
{
Console.WriteLine("Invalid Input of date");
Console.ReadLine();
}
Console.WriteLine("Your Age is " + (String.Format("{0:00}", (now - bday)))); //it rounds off my float
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
}
static void Main(字符串[]args)
{
字符串年、月、日=string.Empty;
Console.WriteLine(“输入您的生日:”);
控制台。写入线(“年份:”;
年份=Console.ReadLine();
Console.WriteLine(“月份:”);
month=Console.ReadLine();
控制台。WriteLine(“日期:”);
day=Console.ReadLine();
尝试
{
DateTime日期=转换为.ToDateTime(年+“-”+月+“-”+天);
var bday=float.Parse(date.ToString(“yyyy.MMdd”);
var now=float.Parse(DateTime.now.ToString(“yyy.MMdd”);
如果(现在
}与评论相反,
TimeSpan
在这方面对你没有帮助,因为一年不是一个固定的时间长度。这反过来会导致你表达的目标非常奇怪。你真的不应该将日期表示为小数,前两位是月,第三位和第四位是天。Ti我不是这样工作的。(例如,考虑一下2014.0131和2014.0201之间的差异远大于2014.0130和2014.0131之间的差异。)
最好用年、月、日来表示一个年龄。我的图库非常简单:
LocalDate birthday = new LocalDate(1976, 6, 19); // For example
LocalDate today = LocalDateTime.FromDateTime(DateTime.Now).Date; // See below
Period period = Period.Between(birthday, today);
Console.WriteLine("You are {0} years, {1} months, {2} days old",
period.Years, period.Months, period.Days);
如果您只想确定年数,您可以决定只使用period.years
,也可以根据period.Months
对结果进行四舍五入
我建议不要在生产代码中使用
DateTime.Now
。然而,在野田佳彦时代,我们有一个IClock
接口,表示“一种及时获取当前瞬间的方法”,在主程序集中使用SystemClock
实现,在测试程序集中使用FakeClock
实现。您的代码将接受IClock
(可能使用依赖项注入)然后用它来确定您感兴趣的时区中的当前日期。这样,您可以在不更改计算机时钟的情况下为任何情况编写测试。这是处理与时间相关的任务的一种好方法。在我们的框架中,我们使用以下方法:
/// <summary>
/// Calculates the age at the specified date.
/// </summary>
/// <param name="dateOfBirth">The date of birth.</param>
/// <param name="referenceDate">The date for which the age should be calculated.</param>
/// <returns></returns>
public static int Age(DateTime dateOfBirth, DateTime referenceDate)
{
int years = referenceDate.Year - dateOfBirth.Year;
dateOfBirth = dateOfBirth.AddYears(years);
if (dateOfBirth.Date > referenceDate.Date)
years--;
return years;
}
/// <summary>
/// Calculates the age at this moment.
/// </summary>
/// <param name="dateOfBirth">The date of birth.</param>
/// <returns></returns>
public static int Age(DateTime dateOfBirth)
{
return Age(dateOfBirth, DateTime.Today);
}
//
///计算指定日期的年龄。
///
///出生日期。
///应计算年龄的日期。
///
公共静态整数年龄(DateTime dateOfBirth,DateTime referenceDate)
{
int YERS=参考日期年-出生日期年;
dateOfBirth=dateOfBirth.AddYears(年);
if(dateOfBirth.Date>referenceDate.Date)
年--;
回归年;
}
///
///计算此时的年龄。
///
///出生日期。
///
公共静态整数年龄(DateTime dateOfBirth)
{
返回年龄(dateOfBirth,DateTime.Today);
}
我找到了问题的答案,我使用LastIndexOf
删除某个字符后面的所有字符串,该字符是点(.),但在此之前我将float
转换为string
准确无误,试试看。:)
static void Main(字符串[]args)
{
字符串年、月、日=string.Empty;
Console.WriteLine(“输入您的生日:”);
控制台。写入线(“年份:”;
年份=Console.ReadLine();
Console.WriteLine(“月份:”);
month=Console.ReadLine();
控制台。WriteLine(“日期:”);
day=Console.ReadLine();
尝试
{
DateTime日期=转换为.ToDateTime(年+“-”+月+“-”+天);
var bday=float.Parse(date.ToString(“yyyy.MMdd”);
var now=float.Parse(DateTime.now.ToString(“yyy.MMdd”);
如果(现在
为什么不使用TimeSpan类?1。您可以使用TimeSpan
。2.看看这个答案,了解如何计算日期yourself@JennyO赖利:你不可能从一个时间跨度到几年……我不清楚你期望的结果是什么——考虑到一年的长度不同,用分数年是没有意义的。以年、月、日为单位进行讨论更有意义。此外,将年-月-日值视为数字
static void Main(string[] args)
{
string year, month, day = string.Empty;
Console.WriteLine("Enter your Birthdate:");
Console.WriteLine("Year :");
year = Console.ReadLine();
Console.WriteLine("Month :");
month = Console.ReadLine();
Console.WriteLine("Day :" );
day = Console.ReadLine();
try
{
DateTime date = Convert.ToDateTime(year + "-" + month + "-" + day);
var bday = float.Parse(date.ToString("yyyy.MMdd"));
var now = float.Parse(DateTime.Now.ToString("yyyy.MMdd"));
if (now < bday)
{
Console.WriteLine("Invalid Input of date");
Console.ReadLine();
}
string age = (now - bday).ToString();
Console.WriteLine("Your Age is " + (age.Substring(0, age.LastIndexOf('.'))));
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
Console.ReadLine();
}
}