如何计算C#的退休日期 [英] How to calculate date of retirement in C#

查看:104
本文介绍了如何计算C#的退休日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#编写一个mysql项目。我想



在60岁时自动生成退休日期



填写日期出生即如果出生日期是15/1/1950那么



退休日期将是2010年1月31日,如果出生日期是



01/1/1950然后退休日期将是31/12/2009一天



之前和它也应该计算闰年。

I am working on an mysql project in C#. I want to

generate the retirement date automatically at the age of 60 years while

filling the date of Birth. i.e. if the Date of birth is 15/1/1950 then

the date of retirement will be 31/1/2010 and if the date of birth is

01/1/1950 then the date of retirement will be 31/12/2009 one day

earlier and it should calculate leap year as well.

推荐答案

DateTime dt = new DateTime(1950, 1, 1);
            TimeSpan day = new TimeSpan(1, 0, 0, 0);
            DateTime retirement = dt.AddYears(60).Subtract(day);
            MessageBox.Show(retirement.ToLongDateString());




string date =01/1 / 1950;

DateTime now = Convert.ToDateTime(date);

DateTime next = now.AddYears(60);



这将给出正确的退休日期,但你需要-1天
Hi,
string date = "01/1/1950";
DateTime now = Convert.ToDateTime(date);
DateTime next = now.AddYears(60);

this will give correct retirement date but you need to -1 day


由于DateTime已经提供了所有必需的功能,你应该能够自己想象该怎么做。例如,调用AddYears(60.0)非常容易。



那么根据您需要的确切逻辑,您可以调用AddMonths和/或AddDays,如果您需要在一个月的开头或月末......你使用当前年份和月份创建另一个DateTime,并将1作为日期。



如果您需要一个月的最后一天,那么您首先计算下个月的第一天,然后减去一天。



例如,要获得一个月的第一天,你需要



Since DateTime already provide all required functions, you should be able to figure by yourself what to do. Fo example, it is very easy to call AddYears(60.0).

Then depending on the exact logic you need you can call AddMonths and/or AddDays and if you need to be at the beginning or end of a month... you create another DateTime with current year and month and 1 as the day.

If you need last day of a month, then you first compute first day of next month and then substract one day.

For example to get first day of a month, you do

DateTime date = something;
var firstDayOfMonth = new DateTime(date.Year, date.Month, 1);





如果需要,您还可以复制有关本地/ UTC时间的信息使用适当的构造函数指向。



要获得该月的最后一天,您可以:



You can also copy information about local/UTC time if desired at that point using the appropriate constructor.

To get the last day of that month, you can do:

var lastDayOfMonth = firstDayOfMonth.AddMonth(1.0).AddDays(-1.0);





为了安全起见,如果你想要一个月的最后一天先获得该月的第一天。如果我们处于下个月不存在的日期,我不确定.NET将如何处理 AddMonths(1)。例如,如果我们将1月份添加到1月的最后一天。



To be on the safe side, if you want last day of a month first get first day of that month. I''m not sure how .NET will handle AddMonths(1) if we are at a date that does not exist in next month. For example, if we add 1 month to last day of January.


这篇关于如何计算C#的退休日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆