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"))


2 comments:

  1. excelente me anduvo perfecto, muchas gracias

    ReplyDelete
  2. 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

    ReplyDelete