Paks
Paks

Reputation: 1470

Get the date of the next week-day (skipping weekends) for displaying data in HTML

I have the following Code:

Public Shared Function DPLoadData() As String
    Dim s As StringBuilder = New StringBuilder("<head><meta http-equiv=""content-type"" content=""text/html;charset=utf-8"" /><META HTTP-EQUIV=""REFRESH"" CONTENT=""900"">")
    s.Append("<style type=""text/css"" media=""all""> body{font-family: Arial;}h4{font-size: 10pt;font-weight: bold;white-space: nowrap;margin-top: 0; margin-bottom: 10px;}")
    s.Append("th{font-size: 9pt;font-weight: normal;text-align: center;white-space: nowrap;}td{font-size: 9pt;}.content td{border: solid 1px #dadada;}")
    s.Append(".content th {border: solid 1px #dadada;background-image: url(""tbl_header_row_bg.gif""); background-repeat: repeat-x; white-space: nowrap;}</style></head>")
    s.Append("<h3>" & "Daily Plan" & "</h3>")
    Dim strCurrDay As String = ""
    s.Append("<h5>" & strCurrDay & "</h5>")

    Dim CurrDateFirstDay As Date = DateAdd(DateInterval.Day, 1, Now)
    strCurrDay = FormatDateTime(CurrDateFirstDay, DateFormat.LongDate)

    s.Append("<h5>" & strCurrDay & "</h5>")
    s.Append(LoadDataGroupByDate(CurrDateFirstDay))
    Return s.ToString()
End Function

The function generates an HTML file with a table and fills it with bookings. Currently, the HTML file displays the bookings of tomorrow (e.g. if today is Monday, it displays the bookings for Tuesday). So far so good. What I want is that if today is Friday, the HTML File should display the bookings for Monday, not Saturday. Is that possible?

My Solution:

        If value.DayOfWeek = DayOfWeek.Friday Then

            Dim CurrDateFirstDay As Date = DateAdd(DateInterval.Day, 3, Now)

            strCurrDay = FormatDateTime(CurrDateFirstDay, DateFormat.LongDate)
            s.Append("<h5>" & strCurrDay & "</h5>")
            s.Append(LoadDataGroupByDate(CurrDateFirstDay))
        Else
            Dim CurrDateFirstDay As Date = DateAdd(DateInterval.Day, 1, Now)

            strCurrDay = FormatDateTime(CurrDateFirstDay, DateFormat.LongDate)
            s.Append("<h5>" & strCurrDay & "</h5>")
            s.Append(LoadDataGroupByDate(CurrDateFirstDay))

        End If

Upvotes: 2

Views: 1005

Answers (1)

Steven Doggart
Steven Doggart

Reputation: 43743

If you create a method like this:

Public Function GetNextWeekDay() As Date
    Dim value As Date = Date.Now
    Do
        value = value.AddDays(1)
    Loop While (value.DayOfWeek = DayOfWeek.Saturday) Or (value.DayOfWeek = DayOfWeek.Sunday)
    Return value
End Function

Then you can set your variable like this:

Dim CurrDateFirstDay As Date = GetNextWeekDay()

Upvotes: 2

Related Questions