Set defualt date as Week Day before Today.

P

Phillip Vong

VB.Net and a simple aspx.net page.

I have this simple code that puts Today's date in these 2 textboxes. Can
some one tell me how to set these two boxes with yesterday's date excluding
weekends?

Thanks!
phil

--------------------------------



Dim dtmToday As String

dtmToday = Now.Date.ToString("MM/d/yyy")

TextBox1.Text = dtmToday

TextBox2.Text = dtmToday
 
P

Phillip Vong

Thanks, but that didn't work. Your code just took the date back 7 days
INCLUDING WEEKENDS. If the textbox is 12/11/06, it is now 12/4/06. If I
change the code to -1, it will go back one day but it does not disregard the
weekends. How do you disregard the weekends?
 
R

Rad [Visual C# MVP]

Thanks, but that didn't work. Your code just took the date back 7 days
INCLUDING WEEKENDS. If the textbox is 12/11/06, it is now 12/4/06. If I
change the code to -1, it will go back one day but it does not disregard the
weekends. How do you disregard the weekends?


Hey Philip,

Try this out. Its not pretty but it works

sub Main
' This should return monday the 11th
Console.WriteLine( _
GetLastBusinessDay(new DateTime(2006,12,12),1))
'This should return friday the 8th
Console.WriteLine( _
GetLastBusinessDay(new DateTime(2006,12,12),2))
Console.ReadLine()
end sub

function GetLastBusinessDay (theDate as DateTime, daysAgo as Integer) as
DateTime
'
' Some local variables
'
dim counter as integer
dim temp as DateTime =theDate
'
' Put in a loop
'
while true
'
' Do some error checking
'
if theDate=DateTime.MinValue
return DateTime.MinValue
end if

'
' Decrement the day
'
temp= temp.AddDays(-1)
'
' Check if it is a weekday
'
select case temp.DayOfWeek
case DayOfWeek.Monday to DayOfWeek.Friday
'
' It is! Increment the counter
'
counter=counter+1
end select
'
' Now check if the counter is equal to
' the days we want
'
if counter=daysago then
return temp
end if

end while
end function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top