Get the Date

danasegarane

Active member
Joined
Jun 14, 2006
Messages
41
Programming Experience
Beginner
Dear All,
How to get the Starting and Ending Date for this conditions
1.CurrentWeek(WeekstartingDate , EndingDate with respect to todaysDate)
2.MonthStart Date and End Date with Respect to Todays date
VB.NET:
'I have this code for WeekStart/EndDate
[COLOR=#007f00]'SerVerDate is date Got From SQlServer[/COLOR]
[COLOR=#007f00][/COLOR] 
 FromDate = ServerDate.AddDays(-1 * ServerDate.DayOfWeek)
 
ToDate = ServerDate.AddDays(7 - ServerDate.DayOfWeek - 1)
[COLOR=#007f00]'I have this code for Month Start/EndDate[/COLOR]
[COLOR=#007f00]'But it returns one day in the nextMonth also.I want Restrict to this month only[/COLOR]
[COLOR=#007f00]'How can I[/COLOR]
FromDate = [COLOR=#00007f]New[/COLOR] DateTime(ServerDate.[COLOR=#00007f]Now[/COLOR].[COLOR=#00007f]Year[/COLOR], ServerDate.[COLOR=#00007f]Now[/COLOR].[COLOR=#00007f]Month[/COLOR], 1)                ToDate = [COLOR=#00007f]New[/COLOR] DateTime(ServerDate.[COLOR=#00007f]Now[/COLOR].[COLOR=#00007f]Year[/COLOR], ServerDate.AddMonths(1).[COLOR=#00007f]Month[/COLOR], 1)
Thanks in Advance
Dana
 
Last edited:
DateTime has a DayOfWeek
0 = Sunday
..
6 = saturday

So

DateTime.Now.AddDays(-1 * DateTime.Now.DayOfWeek) 'back to sunday
DateTime.Now.AddDays(6 - DateTime.Now.DayOfWeek) 'forward to saturday


For the months, Just take the date, and subtract the days from it, or go forward 1 month and back a number of days
DateTime.Now.AddDays(-1 * (DateTime.Now.Day-1)) 'back to the motnh start
DateTime.Now.AddMonths.(1).AddDays(-1 * DateTime.Now.Day) ' forward 1 month,back to the month start
 
Back
Top