在C#/.NET TimeSpan有TotalDays,TotalMinutes等等,但我无法弄清楚总月差的公式.每月可变天数和闰年一直让我失望.我如何获得TotalMonths?
编辑抱歉没有更清楚:我知道我实际上无法得到这个,TimeSpan但我想使用TotalDays,TotalMinutes并将是一个很好的例子来表达我正在寻找...除了我想要得到总月.
示例:2009年12月25日 - 2009年10月6日= 2 TotalMonths.10月6日至11月5日等于0个月.11月6日,1个月.12月6日,2个月
我一直试图写这个循环,它只是变得越来越复杂.基本上我想采取一个给定的日期并循环每个月,直到我达到当月.因此,如果开始日期是11/1/2011,那么我想循环
11/2011,12/2011,1/2012,2/2012等
这是我开始的,但这不起作用.一旦我遇到新的一年,我需要内循环重新开始,而不是startdate.Month.一般来说,循环数月和数年有更好的方法吗?谢谢
for (var y = startdate.Year; y <= DateTime.Now.Year; y++)
{
for (var m = startdate.Month; m <= 12; m++)
{
//do something with m and y
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个函数,它在c#中的两个日期之间给出所有月份名称
List<string> liMonths = MyFunction(date1,date2);
Run Code Online (Sandbox Code Playgroud)
我的功能是
MyFunction(DateTime date1,DateTime date2)
{
//some code
return listOfMonths;
}
Run Code Online (Sandbox Code Playgroud)
你能帮帮我怎么办?