martes, 22 de febrero de 2011

Sumar dias o semanas o meses a una fecha en .net

Cuando estamos desarrollando aplicaciones en .net en ocasiones necesitamos sumar o restar algunas cantidades a las fechas actuales para esto .net trae un metodo el cual nos facilitara mucho la vida


por ejemplo

si quieres sumar un dia a una fecha


Dim ProximoDia as date
Dim DiaActual as date = system.datetime.time.now
ProximoDia = DiaActual .AddDays(1)


Puedes utilizar intelisense para ver los metodos que puedes utilizar para sumer meses años y demas


tambien puedes utilizar el metodo DateAdd



De la documentación de MSDN

DateAdd Function
Returns a Date value containing a date and time value to which a specified time interval has been added.

algunos ejemplos de MSDN

Example

This example takes a date and, using the DateAdd function, displays a corresponding date a specified number of months in the future.


Dim Msg, Number, StartDate As String 'Declare variables.
Dim Months As Double
Dim SecondDate As Date
Dim IntervalType As DateInterval
IntervalType = DateInterval.Month ' Specifies months as interval.
StartDate = InputBox("Enter a date")
SecondDate = CDate(StartDate)
Number = InputBox("Enter number of months to add")
Months = Val(Number)
Msg = "New date: " & DateAdd(IntervalType, Months, SecondDate)
MsgBox (Msg)

1 comentario: