Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Optimize Date function (Only if you are bored)

Status
Not open for further replies.

Qik3Coder

Programmer
Jan 4, 2006
1,487
US
So i have to pre-load a date drop down with the monday before today. I got this working, but was curious if there was a better way to do it.

By the way, I have tests for this and it works for each day of the week.

Code:
    Public Shared Function GetMondayPriorTo(Optional ByVal dteToUse As Date = Nothing) As DateTime
        If Not dteToUse.Date = Nothing Then
            Dim dte As DateTime = dteToUse.Date.AddDays(-1 * (dteToUse.DayOfWeek - DayOfWeek.Monday))
            Return IIf(dte.Date >= dteToUse.Date, dte.AddDays(-7), dte)
        Else
            Return Nothing
        End If
    End Function

Thanks,

-Sometimes the answer to your question is the hack that works
 
I tried, but really, I think you've got the best method already.
 
I wasn't sure if there was something better. I starting looking at it one way, and have trouble stepping back and trying to think of a diff way to attack it.

-Sometimes the answer to your question is the hack that works
 
Well, the important part of your calculation is Dim dte As DateTime = dteToUse.Date.AddDays(-1 * (dteToUse.DayOfWeek - DayOfWeek.Monday)). It just looks more complicated because you have all the extra checking.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top