Display the First and Last Day of Week In ASP.NET

Use the DateTime class to display the First Day and Last of the Week. Here's how

C#


DateTime dt = DateTime.Now;


DateTime wkStDt = DateTime.MinValue;


DateTime wkEndDt = DateTime.MinValue;


wkStDt = dt.AddDays(1 - Convert.ToDouble(dt.DayOfWeek));


wkEndDt = dt.AddDays(7 - Convert.ToDouble(dt.DayOfWeek));


Response.Write(wkStDt.ToString("MMMM-dd") + "<br/>");


Response.Write(wkEndDt.ToString("MMMM-dd"));




VB.NET


Dim dt As DateTime = DateTime.Now


Dim wkStDt As DateTime = DateTime.MinValue


Dim wkEndDt As DateTime = DateTime.MinValue


wkStDt = dt.AddDays(1 - Convert.ToDouble(dt.DayOfWeek))


wkEndDt = dt.AddDays(7 - Convert.ToDouble(dt.DayOfWeek))


Response.Write(wkStDt.ToString("MMMM-dd") & "<br/>")


Response.Write(wkEndDt.ToString("MMMM-dd"))




Will you give this article a +1 ? Thanks in advance


About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

2 comments:

laucaroso said...

excelente me anduvo perfecto, muchas gracias

pipo pipi said...

thankyou
mi codigo ....

Public Class ComprobarDatetime
Public Shared Function fecha(fechaVerificar As DateTime, intervaloTipoComprobar As Integer) As Boolean
Dim StarDate As DateTime
Dim EndDate As DateTime

Select Case intervaloTipoComprobar
Case 1 'anual
StarDate = New DateTime(Date.Now.Year, 1, 1, 1, 1, 1, 1) 'primer milisegundo del año en curso
EndDate = New DateTime(Date.Now.Year, 12, 31, 23, 59, 59, 999) 'ultimo milisegundo del año en curso
Case 2 'mensual
StarDate = New DateTime(Date.Now.Year, Date.Now.Month, 1, 1, 1, 1, 1) 'primer milisegundo del mes en curso
EndDate = New DateTime(Date.Now.Year, Date.Now.Month, DateTime.DaysInMonth(Date.Now.Year, Date.Now.Month), 23, 59, 59, 999) 'ultimo milisegundo del mes en curso
Case 3 ' semanal
Dim dt As DateTime = DateTime.Now

Dim wkStDt As DateTime = DateTime.MinValue

Dim wkEndDt As DateTime = DateTime.MinValue

wkStDt = dt.AddDays(1 - Convert.ToDouble(dt.DayOfWeek))
wkEndDt = dt.AddDays(7 - Convert.ToDouble(dt.DayOfWeek))
StarDate = New DateTime(Date.Now.Year, Date.Now.Month, Convert.ToInt32(wkStDt), 1, 1, 1, 1) 'primer milisegundo de la semana en curso
EndDate = New DateTime(Date.Now.Year, Date.Now.Month, Convert.ToInt32(wkStDt), 23, 59, 59, 999) ' ultimo milisegundo de la semana en curso
Case 4 'diaria
StarDate = New DateTime(Date.Now.Year, Date.Now.Month, Date.Now.Day, 1, 1, 1, 1) 'primer milisegundo del dia en curso
EndDate = New DateTime(Date.Now.Year, Date.Now.Month, Date.Now.Day, 23, 59, 59, 999) ' ultimo milisegundo del dia en curso

Case 5 'hora
StarDate = New DateTime(Date.Now.Year, Date.Now.Month, Date.Now.Day, Date.Now.Hour, 1, 1, 1) 'primer milisegundo de la hora en curso
EndDate = New DateTime(Date.Now.Year, Date.Now.Month, Date.Now.Day, Date.Now.Hour, 59, 59, 999) 'ultimo milisegundo de la hora en curso
Case 6 ' minutos
StarDate = New DateTime(Date.Now.Year, Date.Now.Month, Date.Now.Day, Date.Now.Hour, Date.Now.Minute, 1, 1) 'primer milisegundo del minuto en curso
EndDate = New DateTime(Date.Now.Year, Date.Now.Month, Date.Now.Day, Date.Now.Hour, Date.Now.Minute, 59, 999) 'ultimo milisegundo del minuto en curso
Case 7 ' segundos
StarDate = New DateTime(Date.Now.Year, Date.Now.Month, Date.Now.Day, Date.Now.Hour, Date.Now.Minute, Date.Now.Second, 1) 'primer milisegundo del segundo en curso
EndDate = New DateTime(Date.Now.Year, Date.Now.Month, Date.Now.Day, Date.Now.Hour, Date.Now.Minute, Date.Now.Second, 999) 'ultimo milisegundo del segundo en curso
Case Else
StarDate = New DateTime(1, 1, 1, 1, 1, 1, 1) ' con esto evitamos un error al selecionar algo distinto de los intervalor anteriores
EndDate = New DateTime(1, 1, 1, 1, 1, 1, 1)

End Select
If (fechaVerificar >= StarDate) And (fechaVerificar <= EndDate) Then
Return True
Else
Return False

End If

End Function
End Class