基于当前日期和月数的DateTime计算 [英] DateTime calculation based on current date and number of months

查看:91
本文介绍了基于当前日期和月数的DateTime计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试编写c#代码来计算每个地方花费的总月数的起始日期。我有几个月的时间,我想根据今天的日期和花费的月数来计算开始日期。



如有任何建议,我会很感激,如果有人可以发送我是一个示例代码。在此先感谢。



问候

JS

解决方案

如果你减去从结束日期开始的月数或天数,您将获得开始日期。在DateTime类中有AddDays的方法,我不确定几个月,但如果你的意思是字面数月(无论它们有多少天),你可以使用现有的日期并更改月份,对吗?

试试这个

string ldDateFrom = ldToDay.ToShortDateString();

string ldDateTo = ldToDay.AddDays(31).ToShortDateString();



希望这个帮助



ahmad zrein


Hi,
I am trying to write c# code to calculate starting date from total number of months spent at each place. I have number of months and I want to calculate starting date based on today's date and number of months spent.

Any suggestions please, I will appreciate it if anyone can send me a sample code. Thanks in advance.

Regards
JS

解决方案

If you subtract the number of months or days from the end date, you will get the start date. There's methods on the DateTime class to AddDays, I'm not sure about months, but if you mean literal months ( irrespective of how many days they had), you can just use the existing day and change the month, right ?


try this
string ldDateFrom = ldToDay.ToShortDateString();
string ldDateTo = ldToDay.AddDays(31).ToShortDateString();

hope this help

ahmad zrein


这篇关于基于当前日期和月数的DateTime计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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