Expression, datediffday and week end

3 posts, 1 answers
  1. ERIC
    ERIC avatar
    2 posts
    Member since:
    Jul 2013

    Posted 30 Jul 2013 Link to this post

    Hello,

    Is it possible to count the days separating two dates excluding weekends ?
    Sorry if the topic has already been discussed, I did not find anything similar on the forum

    Thank you in advance !

    Best regards,

    Eric
  2. Answer
    Dimitar
    Admin
    Dimitar avatar
    2907 posts

    Posted 02 Aug 2013 Link to this post

    Hi Eric,

    Thank you for writing.

    To calculate business days only, you can use a custom expression function like the following one:
    public class CustomExpressionContext : Telerik.Data.Expressions.ExpressionContext
    {
        public double GetBusinessDays(DateTime startD, DateTime endD)
        {
           double   calcBusinessDays = 1 + ((endD - startD).TotalDays * 5 - (startD.DayOfWeek - endD.DayOfWeek) * 2) / 7;
     
            if ((int)endD.DayOfWeek == 6)
                calcBusinessDays--;
            if ((int)startD.DayOfWeek == 0)
                calcBusinessDays--;
     
            return calcBusinessDays;
        }
    }

    You can apply the function to the column like this:
    Telerik.Data.Expressions.ExpressionContext.Context = new CustomExpressionContext();
    this.radGridView1.Columns[2].Expression = "GETBUSINESSDAYS(col1,col2)";

    I hope this information helps. Should you have any other questions, I will be glad to assist you.

    Regards,
    Dimitar
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
  3. ERIC
    ERIC avatar
    2 posts
    Member since:
    Jul 2013

    Posted 02 Aug 2013 Link to this post

    Hi,

    Thank you very much it works as desired!

    Best regards,

    Eric
Back to Top