Misu Egri
Misu Egri

Reputation: 43

Working with calendar in visual basic

How to use calendar for the lower time format in vb?
What i try to achieve: if the lower date is saturday and sunday replace it for friday same week (if date = 2012-04-21 or 2012-04-22 the date = 2012-04-20).

2012-04-20T18:26:43

Thanks in advance

Upvotes: 1

Views: 1132

Answers (2)

Steve
Steve

Reputation: 216333

Just put together some code without too much test.

Sub Main
    Dim dt as DateTime = FindDate(Convert.ToDateTime("2012-04-21T18:26:43"))    
    System.Diagnostics.Debug.WriteLine(dt.ToShortDateTime())
End Sub

Public Function FindDate(ByVal inDate as DateTime) as DateTime 
    if (inDate.DayOfWeek = DayOfWeek.Saturday Or inDate.DayOfWeek = DayOfWeek.Sunday) then 
       while (inDate.DayOfWeek <> DayOfWeek.Friday) 
          inDate = inDate.AddDays(-1) 
       end while
    end if 
    return inDate 
End Function 

Upvotes: 1

Hans Passant
Hans Passant

Reputation: 942187

Public Shared Function NoWeekend(ByVal day As DateTime) As DateTime
    If day.DayOfWeek = DayOfWeek.Sunday Then Return day.AddDays(-2)
    If day.DayOfWeek = DayOfWeek.Saturday Then Return day.AddDays(-1)
    Return day
End Function

Upvotes: 1

Related Questions