从给定日期添加特定日期一年

时间:2015-12-15 12:59:18

标签: c# date datetime

我的开始日期类似于“2015-03-10”。我想从这个开始日期开始每月增加1.25天。例如,我的开始日期为“2015-03-10”,然后今年的天数为12.50。 (从3月份起每月1.25天)。

鉴于日期:2015-11-10需要添加截至2016年3月31日的天数:1.25 * 5(从11月份开始,这里为5)(每月增加1.25天)今年的天数:6.25 (年初计算从4月开始计算,年末计算为3月。)

我怎样才能在c#中做到这一点,任何人都可以帮助我做到这一点  提前致谢

2 个答案:

答案 0 :(得分:1)

这个例子根据你的帖子忽略了几个月的日子:

DateTime start;
DateTime end;

// assuming end > start
double value = 1.25 * (end.Month - start.Month + 12 * (end.Year - start.Year) + 1);
编辑:我可以帮助您在一些步骤中理解这行代码拆分:

DateTime start = DateTime.Now;
DateTime end = new DateTime(start.Year, 3, 31);

if (start.Month > 3) 
    end = end.AddYear(1);

double years = end.Year - start.Year;
double months = end.Month - start.Month + 1;
months += (years * 12);
double value = 1.25 * months;

答案 1 :(得分:0)

我是通过以下方式得到的:

DateTime endDate = new DateTime(DateTime.Today.Year + 1, 4, 1).AddDays(-1);
if (Convert.ToDateTime(empHiredDate).Month > 4)
{
    finMonth = Convert.ToDateTime(empHiredDate).Month - 4;
    finMonth = 12 - finMonth;
    avail =Convert.ToString(finMonth * 1.25);
}
else if (Convert.ToDateTime(empHiredDate).Month < 3)
{
    finMonth = Convert.ToDateTime(empHiredDate).Month + 8;
    finMonth = 12 - finMonth;
    avail = Convert.ToString(finMonth * 1.25);
}
else if (Convert.ToDateTime(empHiredDate).Month == 4)
{
    avail = Convert.ToString(12 * 1.25);
}
else if (Convert.ToDateTime(empHiredDate).Month == 3)
{
    avail = Convert.ToString(1 * 1.25);
}
相关问题