Obtain the workday (except Saturday and Sunday) between dates)

Source: Internet
Author: User

Print? /// <Summary>
/// Obtain the workday in the date segment [except Saturday and Sunday]
/// </Summary>
/// <Param name = "startDate"> </param>
/// <Param name = "endDate"> </param>
/// <Returns> </returns>
Public static long dateDiff (String startDate, String endDate)
{
 
DateTime fromTime = CommonFunc. StringToDate (startDate );
DateTime toTime = CommonFunc. StringToDate (endDate );
TimeSpan ts = toTime. Subtract (fromTime); // TimeSpan obtains the time interval between fromTime and toTime.
Long countday = ts. Days; // obtain the total number of Days in the two-day period
Long weekday = 0; // workday
// The cycle is used to deduct the weekends in the total number of days
For (int I = 0; I <countday; I ++)
{
DateTime tempdt = fromTime. Date. AddDays (I + 1 );
If (tempdt. DayOfWeek! = System. DayOfWeek. Saturday & tempdt. DayOfWeek! = System. DayOfWeek. Sunday)
{
Weekday ++;
}
}
 
Return weekday;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.