我该如何计算日期 [英] how can i calculate date
问题描述
如何获取当前日期,然后计算另一个日期?
how can i get the current date and then calculate another date ??
公式:当前日期(dd-mm-yyyy HH:MM:SS) - (月份,如果是1年,然后是12个月)
formula : current date (dd-mm-yyyy HH:MM:SS) - (month, if 1year then 12 month)
如何根据当前日期计算日期
how to caculate the date based on current date
当前日期 - 没有。月份
current date - no. of month
例如:如果今天是(04-07-2010) - 12个月那么结果应该是04-07-2009
for example: if today is (04-07-2010) - 12 month then the result should be 04-07-2009
推荐答案
我不确定你想要答案的语言是什么。 但这里是C#。
Hi, I'm not sure what language you want the answer in. But here it is in C#.
DateTime myDate = new DateTime(2010, 4, 7);
DateTime newDate = myDate.AddMonths(-12);
DateTime Now = DateTime.Now;
newDate将包含日期04-07-2009,而Now包含创建对象的日期/时间。  ;只是想补充说明你需要它,因为你提到问题中的当前日期。
newDate would contain the date 04-07-2009, and Now contains the date/time when the object is created. Just wanted to add that incase you would need it since you were mentioning current date in the question.
编辑: 注意AddMonths函数调用中的-12,您可以使用正数或负数来调整日期。 还有其他功能,包括年,日,小时等。
edit: Notice the -12 in the AddMonths function call, you can use positive or negative numbers to adjust dates. There are other functions for year, day, hour and so on.
这篇关于我该如何计算日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!