Date calculation,

Posted on 2011-05-09
Last Modified: 2012-05-11
Hi, Experts

I have problem to auto set the date for example:-

Due Date = 31-May-2011, Then Date A is 1 day before the due date, Then Date B is 2 days before Date A, Then Date C is 14 days before Due Date. How can i get the date which is exclude Sunday.

Below code is the sample code provided by CodeCruiser that previously i use in my program and now i need more advance calculation :

Dim sDate As Date = Date.Parse("30/03/2010")
                Dim dayCount As Integer = 10
                For i As Integer = 1 To dayCount
                    sDate = sDate.AddDays(-1)
                    If sDate.DayOfWeek = DayOfWeek.Sunday Then
                        sDate = sDate.AddDays(-1)
                    End If


Question by:chkueh
    LVL 53

    Accepted Solution

    Just put it into a function, with variable startDate and integer as days

            Dim dueDate As Date = Date.Parse("30/03/2010")
            Dim dateA As Date = ReturnDate(dueDate, 1)
            Dim dateB As Date = ReturnDate(dateA, 2)
            Dim dateC As Date = ReturnDate(dueDate, 14)
    Private Function ReturnDate(ByVal startDate As Date, ByVal days As Integer) As Date
            Dim sDate As Date = Date.Parse(startDate)
            For i As Integer = 1 To days
                sDate = sDate.AddDays(-1)
                If sDate.DayOfWeek = DayOfWeek.Sunday Then
                    sDate = sDate.AddDays(-1)
                End If
            Return sDate
        End Function

    Open in new window


    Author Comment

    Thanks for your help, Dhaest...

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now