如何根据持续时间和开始日期计算日期 [英] How to calculate the date based on duration and start Date

查看:192
本文介绍了如何根据持续时间和开始日期计算日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想根据Atrt日期和持续时间来计算结束日期.在这里,我要避免星期六和星期日,并且要计算剩余天数.如何在Javascript和C#中做到这一点.例如,持续时间为7,开始日期为2012年6月29日
那么答案应为2012年8月7日,应避免使用星期六和星期日.



在此先感谢

Hi,

I want to calculate the end date based on Atrt date and duration. Here I want to avoid the Saturday and Sunday and I want to calculate remaining days. How to do this one in Javascript and also in C#. Example the Duration is 7 and start date is 29/06/2012
Then the answer should come as 08/07/2012 It should avoid the Saturday and Sunday.



Thanks in Advance

推荐答案

^ ]可能会帮助您入门, [
This[^] will help to get you started as might this[^].


假设您仅添加整个日期,而忽略诸如时间变化之类的内容,则应执行以下类似操作:
Assuming that you add whole date only and ignore thing like time changes, something similar to this should do:
int daysTillStaturday = (int)DayOfWeek.Saturday - (int)startDate.DayOfWeek;
if (durationInDays < daysTillStaturday)
    return startDate.AddDays(durationInDays);

int remainingDays = durationInDays - daysTillStaturday;

// Skip first weekend and an extra one for each 5 days.
int weekendsCount = 1 + (remainingDays / 5);

// Compute final date by adding extra weekend days...
return startDate.AddDays(durationInDays + 2 * weekendsCount);


这篇关于如何根据持续时间和开始日期计算日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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