Cuenta regresiva a partir de una fecha (VB.Net)

(1/2) > >>

#Aitor:
Código
   Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
       '  Dim Año As Integer = Microsoft.VisualBasic.DateAndTime.Year(Now) '2015
 
       Dim Mes As Integer = Microsoft.VisualBasic.DateAndTime.Month(Now) ' Mes 9
       Dim Dia As Integer = Microsoft.VisualBasic.DateAndTime.Day(Now)
       Dim Hora As Integer = Microsoft.VisualBasic.DateAndTime.Hour(Now)
       Dim Minuto As Integer = Microsoft.VisualBasic.DateAndTime.Minute(Now)
       Dim Segundo As Integer = Microsoft.VisualBasic.DateAndTime.Second(Now)
 
 
       Dim MesAni As Integer = 10
       Dim DiaAni As Integer = 5
       Dim HoraAni As Integer = 23
       Dim MinutoAni As Integer = 59
       Dim SegundoAni As Integer = 59
 
       Dim Mesrestante As Integer = MesAni - Mes
       Dim Diarestante As Integer = DiaAni - Dia
       Dim Horarestante As Integer = HoraAni - Hora
       Dim Minutorestante As Integer = MinutoAni - Minuto
       Dim Segundorestante As Integer = SegundoAni - Segundo
 
       Label1.Text = "Dias: " & Diarestante & " Horas: " & Horarestante & " Minutos " & Minutorestante & " Segundos: " & Segundorestante
       Label1.Show()
 
   End Sub


La idea es hacer una cuenta regresiva que este en una continua actualización, a partir de una fecha clave, y la fecha base del ordenador.

El problema aquí está en que el mes no es el mismo, y por lo tanto la variable Diarestante da un valor negativo. ¿Cómo solucionar eso? O en su defecto alguna forma mejor de hacerlo.

La idea principal es que no se muestre lo que contiene el programa hasta ese día, supongo que para saber si se ha pasado el día basta con comprobar que el díaActual sea mayor que el diaAni.


Edito:

Para arreglarlo de una forma cutre:

Código
       Dim Mes As Integer = Microsoft.VisualBasic.DateAndTime.Month(Now) ' Mes 9
       Dim Dia As Integer = Microsoft.VisualBasic.DateAndTime.Day(Now)
       Dim Hora As Integer = Microsoft.VisualBasic.DateAndTime.Hour(Now)
       Dim Minuto As Integer = Microsoft.VisualBasic.DateAndTime.Minute(Now)
       Dim Segundo As Integer = Microsoft.VisualBasic.DateAndTime.Second(Now)
 
 
       Dim MesAni As Integer = 10
       Dim DiaAni As Integer = 278
       Dim HoraAni As Integer = 23
       Dim MinutoAni As Integer = 59
       Dim SegundoAni As Integer = 59
 
       Select Case Dia
           Case 23
               Dia = 266
           Case 24
               Dia = 267
           Case 25
               Dia = 268
           Case 26
               Dia = 269
           Case 27
               Dia = 270
           Case 28
               Dia = 271
           Case 29
               Dia = 272
           Case 30
               Dia = 273
           Case 1
               Dia = 274
           Case 2
               Dia = 275
           Case 3
               Dia = 276
           Case 4
               Dia = 277
           Case 5
               Dia = 278
       End Select
 
       Dim Mesrestante As Integer = MesAni - Mes
       Dim Diarestante As Integer = DiaAni - Dia
       Dim Horarestante As Integer = HoraAni - Hora
       Dim Minutorestante As Integer = MinutoAni - Minuto
       Dim Segundorestante As Integer = SegundoAni - Segundo
 
 
       Label1.Text = "Dias: " & Diarestante & " Horas: " & Horarestante & " Minutos " & Minutorestante & " Segundos: " & Segundorestante
       Label1.Show()

De esta forma con los casos me aseguro de que tengan el dia correspondiente a su dia en base a los 365 dias posibles...

Pero claro, esto solo sirve como un apaño para solucionar mi problema suponiendo que la cuenta regresiva es de máximo 1 mes. Sigo sin saber como solucionar el problema en sí.

Eleкtro:
Cita de: #Aitor en 23 Septiembre 2015, 16:26 pm

¿Cómo solucionar eso? O en su defecto alguna forma mejor de hacerlo.

Estás siguiendo el camino equivocado.

Usa un TimeSpan.


Tenia un viejo código que desarrollé el año pasado para medir el tiempo de forma amistosa, para usar un cronómetro o un temporizador/cuenta atrás de forma sencilla. He refactorizado un poco el código (por eso he tardado en responderte).

Para tus necesidades, podrías utilizarlo de la siguiente manera:
Código
Public NotInheritable Class Form1 : Inherits Form
 
   ''' <summary>
   ''' The <see cref="TimeMeasurer"/> instance that measure time intervals.
   ''' </summary>
   Private WithEvents countDown As New TimeMeasurer With {.UpdateInterval = 100}
 
   Private Shadows Sub Load() Handles MyBase.Load
 
       ' Medir el lapso de 1 mes desde la fecha y hora actual.
       Me.countDown.Start(DateTime.Now, DateTime.Now.AddMonths(1))
 
   End Sub
 
   ''' <summary>
   ''' Handles the TimeUpdated event of the countdown instance.
   ''' </summary>
   ''' <param name="sender">The source of the event.</param>
   ''' <param name="e">The <see cref="TimeMeasurer.TimeUpdatedEventArgs"/> instance containing the event data.</param>
   Private Sub Countdown_TimeUpdated(ByVal sender As Object, ByVal e As TimeMeasurer.TimeUpdatedEventArgs) _
   Handles countDown.TimeUpdated
 
       Me.lblCountDown.Text = String.Format("Days:{0:00} Hours:{1:00} Minutes:{2:00} Seconds:{3:00}",
                                           e.Remaining.Days, e.Remaining.Hours, e.Remaining.Minutes, e.Remaining.Seconds)
 
   End Sub
 
End Class


El código fuente (version actualizada y mejor refactorizada):
Código
' ***********************************************************************
' Author           : Elektro
' Last Modified On : 26-September-2015
' ***********************************************************************
' <copyright file="TimeMeasurer.vb" company="Elektro Studios">
'     Copyright (c) Elektro Studios. All rights reserved.
' </copyright>
' ***********************************************************************
 
#Region " Usage Examples "
 
#End Region
 
#Region " Option Statements "
 
Option Strict On
Option Explicit On
Option Infer Off
 
#End Region
 
#Region " Imports "
 
Imports System
Imports System.Diagnostics
Imports System.Linq
Imports System.Windows.Forms
 
#End Region
 
''' ----------------------------------------------------------------------------------------------------
''' <summary>
''' Measures the elapsed and/or remaining time of a time interval.
''' The time measurer can be used as a chronometer or a countdown.
''' </summary>
''' ----------------------------------------------------------------------------------------------------
Public NotInheritable Class TimeMeasurer : Implements IDisposable
 
#Region " Objects "
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' An <see cref="Stopwatch"/> instance to retrieve the elapsed time. (chronometer)
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   Private timeElapsed As Stopwatch
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' A <see cref="TimeSpan"/> instance to retrieve the remaining time. (countdown)
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   Private timeRemaining As TimeSpan
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' A <see cref="Timer"/> instance that updates the elapsed and remaining time, and also raise <see cref="TimeMeasurer"/> events.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   Private WithEvents measureTimer As Timer
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Flag that indicates wheter this <see cref="TimeMeasurer"/> instance has finished to measure time interval.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   Private isFinished As Boolean
 
#End Region
 
#Region " Properties "
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Gets the maximum time that the <see cref="TimeMeasurer"/> can measure, in milliseconds.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   ''' <value>
   ''' The maximum time that the <see cref="TimeMeasurer"/> can measure, in milliseconds.
   ''' </value>
   ''' ----------------------------------------------------------------------------------------------------
   Public Shared ReadOnly Property MaxValue As Double
       Get
           Return (TimeSpan.MaxValue.TotalMilliseconds - 1001.0R)
       End Get
   End Property
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Gets the current state of this <see cref="TimeMeasurer"/> instance.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   ''' <value>
   ''' The update interval.
   ''' </value>
   ''' ----------------------------------------------------------------------------------------------------
   Public ReadOnly Property State As TimeMeasurerState
       Get
           If (Me.timeElapsed Is Nothing) OrElse (Me.isFinished) Then
               Return TimeMeasurerState.Disabled
 
           ElseIf Not Me.timeElapsed.IsRunning Then
               Return TimeMeasurerState.Paused
 
           Else
               Return TimeMeasurerState.Enabled
 
           End If
       End Get
   End Property
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Gets or sets the update interval.
   ''' Maximum value is 1000 (1 second).
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   ''' <value>
   ''' The update interval.
   ''' </value>
   ''' ----------------------------------------------------------------------------------------------------
   ''' <exception cref="ArgumentException">
   ''' A value smaller than 1000 is required.;value
   ''' </exception>
   Public Property UpdateInterval As Integer
       Get
           Return Me.updateIntervalB
       End Get
       <DebuggerHidden>
       <DebuggerStepThrough>
       Set(ByVal value As Integer)
           If (value > 1000) Then
               Throw New ArgumentException(message:="A value smaller than 1000 is required.", paramName:="value")
           Else
               Me.updateIntervalB = value
               If (Me.measureTimer IsNot Nothing) Then
                   Me.measureTimer.Interval = value
               End If
           End If
       End Set
   End Property
   ''' <summary>
   ''' The update interval.
   ''' </summary>
   Private updateIntervalB As Integer = 100I
 
#End Region
 
#Region " Enumerations "
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Specifies the current state of a <see cref="TimeMeasurer"/> instance.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   Public Enum TimeMeasurerState As Integer
 
       ''' <summary>
       ''' The <see cref="TimeMeasurer"/> is running.
       ''' </summary>
       Enabled = &H0I
 
       ''' <summary>
       ''' The <see cref="TimeMeasurer"/> is paused.
       ''' </summary>
       Paused = &H1I
 
       ''' <summary>
       ''' The <see cref="TimeMeasurer"/> is fully stopped, it cannot be resumed.
       ''' </summary>
       Disabled = &H2I
 
   End Enum
 
#End Region
 
#Region " Events "
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Occurs when the elapsed/remaining time updates.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   Public Event TimeUpdated(ByVal sender As Object, ByVal e As TimeUpdatedEventArgs)
 
#Region " Time Updated EventArgs "
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Defines the <see cref="TimeUpdated"/> event arguments.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   Public NotInheritable Class TimeUpdatedEventArgs : Inherits EventArgs
 
#Region " Properties "
 
       ''' ----------------------------------------------------------------------------------------------------
       ''' <summary>
       ''' Gets the elapsed time.
       ''' </summary>
       ''' ----------------------------------------------------------------------------------------------------
       ''' <value>
       ''' The elapsed time.
       ''' </value>
       ''' ----------------------------------------------------------------------------------------------------
       Public ReadOnly Property Elapsed As TimeSpan
           Get
               Return Me.elapsedB
           End Get
       End Property
       ''' ----------------------------------------------------------------------------------------------------
       ''' <summary>
       ''' ( Baking Field )
       ''' The elapsed time.
       ''' </summary>
       ''' ----------------------------------------------------------------------------------------------------
       Private ReadOnly elapsedB As TimeSpan
 
       ''' ----------------------------------------------------------------------------------------------------
       ''' <summary>
       ''' Gets the remaining time.
       ''' </summary>
       ''' ----------------------------------------------------------------------------------------------------
       ''' <value>
       ''' The remaining time.
       ''' </value>
       ''' ----------------------------------------------------------------------------------------------------
       Public ReadOnly Property Remaining As TimeSpan
           Get
               Return Me.remainingB
           End Get
       End Property
       ''' ----------------------------------------------------------------------------------------------------
       ''' <summary>
       ''' ( Baking Field )
       ''' The remaining time.
       ''' </summary>
       ''' ----------------------------------------------------------------------------------------------------
       Private ReadOnly remainingB As TimeSpan
 
       ''' ----------------------------------------------------------------------------------------------------
       ''' <summary>
       ''' Gets the goal time.
       ''' </summary>
       ''' ----------------------------------------------------------------------------------------------------
       ''' <value>
       ''' The goal time.
       ''' </value>
       ''' ----------------------------------------------------------------------------------------------------
       Public ReadOnly Property Goal As TimeSpan
           Get
               Return Me.goalB
           End Get
       End Property
       ''' ----------------------------------------------------------------------------------------------------
       ''' <summary>
       ''' ( Baking Field )
       ''' The goal time.
       ''' </summary>
       ''' ----------------------------------------------------------------------------------------------------
       Private ReadOnly goalB As TimeSpan
 
#End Region
 
#Region " Constructors "
 
       ''' ----------------------------------------------------------------------------------------------------
       ''' <summary>
       ''' Prevents a default instance of the <see cref="TimeUpdatedEventArgs"/> class from being created.
       ''' </summary>
       ''' ----------------------------------------------------------------------------------------------------
       Private Sub New()
       End Sub
 
       ''' ----------------------------------------------------------------------------------------------------
       ''' <summary>
       ''' Initializes a new instance of the <see cref="TimeUpdatedEventArgs"/> class.
       ''' </summary>
       ''' ----------------------------------------------------------------------------------------------------
       ''' <param name="elapsed">
       ''' The elapsed time.
       ''' </param>
       '''
       ''' <param name="remaining">
       ''' The remaining time.
       ''' </param>
       '''
       ''' <param name="goal">
       ''' The goal time.
       ''' </param>
       ''' ----------------------------------------------------------------------------------------------------
       Public Sub New(ByVal elapsed As TimeSpan,
                      ByVal remaining As TimeSpan,
                      ByVal goal As TimeSpan)
 
           Me.elapsedB = elapsed
           Me.remainingB = remaining
           Me.goalB = goal
 
       End Sub
 
#End Region
 
   End Class
 
#End Region
 
#End Region
 
#Region " Public Methods "
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Starts the time interval measurement.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   ''' <param name="milliseconds">
   ''' The time interval to measure, in milliseconds.
   ''' </param>
   ''' ----------------------------------------------------------------------------------------------------
   ''' <exception cref="ArgumentOutOfRangeException">
   ''' milliseconds;A value smaller than <see cref="TimeMeasurer.MaxValue"/> is required.
   ''' </exception>
   '''
   ''' <exception cref="ArgumentOutOfRangeException">
   ''' milliseconds;A value greater than 0 is required.
   ''' </exception>
   ''' ----------------------------------------------------------------------------------------------------
   <DebuggerHidden>
   <DebuggerStepThrough>
   Public Sub Start(ByVal milliseconds As Double)
 
       If (milliseconds > TimeMeasurer.MaxValue) Then
           Throw New ArgumentOutOfRangeException(paramName:="milliseconds",
                                                 message:=String.Format("A value smaller than {0} is required.", TimeMeasurer.MaxValue))
 
       ElseIf (milliseconds <= 0) Then
           Throw New ArgumentOutOfRangeException(paramName:="milliseconds",
                                                 message:="A value greater than 0 is required.")
 
       Else
           Me.timeElapsed = New Stopwatch
           Me.timeRemaining = TimeSpan.FromMilliseconds(milliseconds)
           Me.measureTimer = New Timer With
              {
                .Tag = milliseconds,
                .Interval = Me.updateIntervalB,
                .Enabled = True
              }
 
           Me.isFinished = False
           Me.timeElapsed.Start()
           Me.measureTimer.Start()
 
       End If
 
   End Sub
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Starts a time interval measurement given a difference between two dates.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   ''' <param name="startDate">
   ''' The starting date.
   ''' </param>
   '''
   ''' <param name="endDate">
   ''' The ending date.
   ''' </param>
   ''' ----------------------------------------------------------------------------------------------------
   <DebuggerHidden>
   <DebuggerStepThrough>
   Public Sub Start(ByVal startDate As Date,
                    ByVal endDate As Date)
 
       Me.Start(endDate.Subtract(startDate).TotalMilliseconds)
 
   End Sub
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Starts a time interval measurement given a <see cref="TimeSpan"/>.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   ''' <param name="time">
   ''' A <see cref="TimeSpan"/> instance that contains the time interval.
   ''' </param>
   ''' ----------------------------------------------------------------------------------------------------
   <DebuggerHidden>
   <DebuggerStepThrough>
   Public Sub Start(ByVal time As TimeSpan)
 
       Me.Start(time.TotalMilliseconds)
 
   End Sub
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Pauses the time interval measurement.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   ''' <exception cref="Exception">
   ''' TimeMeasurer is not running.
   ''' </exception>
   ''' ----------------------------------------------------------------------------------------------------
   <DebuggerHidden>
   <DebuggerStepThrough>
   Public Sub Pause()
 
       If (Me.State <> TimeMeasurerState.Enabled) Then
           Throw New Exception("TimeMeasurer is not running.")
 
       Else
           Me.measureTimer.Stop()
           Me.timeElapsed.Stop()
 
       End If
 
   End Sub
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Resumes the time interval measurement.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   ''' <exception cref="Exception">
   ''' TimeMeasurer is not paused.
   ''' </exception>
   ''' ----------------------------------------------------------------------------------------------------
   <DebuggerHidden>
   <DebuggerStepThrough>
   Public Sub [Resume]()
 
       If (Me.State <> TimeMeasurerState.Paused) Then
           Throw New Exception("TimeMeasurer is not paused.")
 
       Else
           Me.measureTimer.Start()
           Me.timeElapsed.Start()
 
       End If
 
   End Sub
 
   ''' <summary>
   ''' Stops the time interval measurement.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   ''' <exception cref="Exception">
   ''' TimeMeasurer is not running.
   ''' </exception>
   ''' ----------------------------------------------------------------------------------------------------
   <DebuggerHidden>
   <DebuggerStepThrough>
   Public Sub [Stop]()
 
       If (Me.State = TimeMeasurerState.Disabled) Then
           Throw New Exception("TimeMeasurer is not running.")
 
       Else
           Me.Reset()
           Me.isFinished = True
 
           Me.measureTimer.Stop()
           Me.timeElapsed.Stop()
 
       End If
 
   End Sub
 
#End Region
 
#Region " Private Methods "
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Stops Time intervals and resets the elapsed and remaining time to zero.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   Private Sub Reset()
 
       Me.measureTimer.Stop()
       Me.timeElapsed.Reset()
 
   End Sub
 
#End Region
 
#Region " Event Handlers "
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Handles the <see cref="Timer.Tick"/> event of the <see cref="MeasureTimer"/> timer.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   ''' <param name="sender">
   ''' The source of the event.
   ''' </param>
   '''
   ''' <param name="e">
   ''' The <see cref="EventArgs"/> instance containing the event data.
   ''' </param>
   ''' ----------------------------------------------------------------------------------------------------
   Private Sub MeasureTimer_Tick(ByVal sender As Object, ByVal e As EventArgs) _
   Handles measureTimer.Tick
 
       Dim timeDiff As TimeSpan = (Me.timeRemaining - Me.timeElapsed.Elapsed)
 
       ' If finished...
       If (timeDiff.TotalMilliseconds <= 0.0R) _
       OrElse (Me.timeElapsed.ElapsedMilliseconds > DirectCast(Me.measureTimer.Tag, Double)) Then
 
           Me.Reset()
           Me.isFinished = True
 
           If (Me.TimeUpdatedEvent IsNot Nothing) Then
               Dim goal As TimeSpan = TimeSpan.FromMilliseconds(DirectCast(Me.measureTimer.Tag, Double))
               RaiseEvent TimeUpdated(sender, New TimeUpdatedEventArgs(goal, TimeSpan.FromMilliseconds(0.0R), goal))
           End If
 
       Else ' If not finished...
           If (Me.TimeUpdatedEvent IsNot Nothing) Then
               RaiseEvent TimeUpdated(sender, New TimeUpdatedEventArgs(Me.timeElapsed.Elapsed,
                                                                       timeDiff,
                                                                       TimeSpan.FromMilliseconds(DirectCast(Me.measureTimer.Tag, Double))))
           End If
 
       End If
 
   End Sub
 
#End Region
 
#Region " IDisposable Support "
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' To detect redundant calls when disposing.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   Private isDisposed As Boolean = False
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Releases all the resources used by this instance.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   <DebuggerStepThrough>
   Public Sub Dispose() Implements IDisposable.Dispose
       Me.Dispose(isDisposing:=True)
       GC.SuppressFinalize(obj:=Me)
   End Sub
 
   ''' ----------------------------------------------------------------------------------------------------
   ''' <summary>
   ''' Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
   ''' Releases unmanaged and - optionally - managed resources.
   ''' </summary>
   ''' ----------------------------------------------------------------------------------------------------
   ''' <param name="isDisposing">
   ''' <see langword="True"/> to release both managed and unmanaged resources;
   ''' <see langword="False"/> to release only unmanaged resources.
   ''' </param>
   ''' ----------------------------------------------------------------------------------------------------
   <DebuggerStepThrough>
   Private Sub Dispose(ByVal isDisposing As Boolean)
 
       If (Not Me.isDisposed) AndAlso (isDisposing) Then
 
           If (Me.measureTimer IsNot Nothing) Then
               Me.measureTimer.Stop()
               Me.measureTimer.Dispose()
           End If
 
           If (Me.TimeUpdatedEvent IsNot Nothing) Then
               RemoveHandler Me.TimeUpdated, Me.TimeUpdatedEvent
           End If
 
       End If
 
       Me.isDisposed = True
 
   End Sub
 
#End Region
 
End Class

#Aitor:
Siento haberte hecho modificar parte de tu código, pero mi nivel de programación es pésimo y no entiendo nada...

Estoy en shock (?)

Eleкtro:
Cita de: #Aitor en 23 Septiembre 2015, 18:53 pm

Siento haberte hecho modificar parte de tu código, pero mi nivel de programación es pésimo y no entiendo nada...

Estoy en shock (?)


La class TimeMeasurer que compartí, solo tienes que copiarla y pegarla en un nuevo archivo.vb, el archivo lo creas desde Visual Studio, Add Item -> Class, y ahí pegas todo el código tal cual lo puse.

La class expone varios eventos, los que te interesan son TimeMeasurer.RemainingTimeUpdated y TimeMeasurer.RemainingTimeFinished, el primer evento se dispara al mismo tiempo que el Timer interno de la class tickea (el intervalo de actualización lo puedes modificar usando la propiedad TimeMeasurer.UpdateInterval), y el otro evento se dispara cuando la cuenta atrás llega a cero.

Arriba te mostré un ejemplo de lo que acabo de mencionar respecto a cómo suscribirte a esos eventos y cómo usar la class en general, ¿hay algo que sigas sin entender?.

Saludos!

Lekim:
Hola Aitor

Te explico, respondí a tu pregunta en el foro de VisualBasic, de hecho al final te decía que eso no era para .Net.  así que he copiado la respuesta y ahora la pongo aquí (añadiendo alguna cosa).

Veo que ya te ha respondido Elektro, pero él es un programador muy avanzado y a veces su código puede ser un poco difícil de entender como ya has mencionado. Yo soy más como tu así que no creo que tengas problemas para entender el mío.



Código
   Label1.Text = "Dias: " & (-1 * Diarestante) & " Horas: " & Horarestante & " Minutos " & Minutorestante & " Segundos: " & Segundorestante


No soy profesional pero sabía un poco sobre el tema porque hice un programa en el que tenía que ir restando días a partir de una determinada fecha.  Así que he hecho esta otra forma. Creo que se puede simplificar más o hacer de otra manera.


Ejemplo, la línea siguiente resta 3 días a la fecha 5/8/2015 y se obtiene 3/12/2015:
Código
       Dim Fecha As Date = "#5/12/2015#"
       Me.Text = DateAdd(DateInterval.Day, -2, Fecha)
 

Esta otra obtiene la diferéncia en días, meses, años, horas, etc te sadrá valor negativo o postitivo dependiendo de si Date1 es anterior o posterior a Date2. En este caso obtiene días porque he puesto DateInterval.Day, si quieres meses tienes que poner DateInterval.Month, y así sucesivamente:
Código
DateAndTime.DateDiff(DateInterval.Day, Date1, Date2)
 

Espero que te sirva, sino para lo que quieres que sirva como ejemplo.

Código
'//////////////////
'////Por Lekim ////
'///29/09/2015/////
'//////////////////
Option Explicit On
Option Infer Off
Option Strict On
Public Class Form1
 
   Dim DiaMenos As Integer
   Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
       '  Dim Año As Integer = Microsoft.VisualBasic.DateAndTime.Year(Now) '2015
 
       Dim FechaLim As Object = "#5/10/2015#" 'Fecha futura límite
       Dim Reloj24h As Object = "#23:59:59#" '24 horas del día a restar
       Dim RelojNow As Object = DateAndTime.TimeValue(CStr(Date.Now))
       Dim TotalDias As Integer = CInt(DateAndTime.DateDiff(DateInterval.Day, CDate(Date.Now), CDate(FechaLim))) 'Días hasta la fecha límite
       Dim DiasRestantes As Integer 'Dias restante que quedan
       Dim CronoTime As Date 'Nueva tiempo resultado de ir restandos segundos
       Dim CronoFecha As Object = String.Empty 'Nueva fecha resultado de ir restando días
 
       Dim m As Integer : Dim h As Integer : Dim s As Integer
       h = CInt((DateDiff(DateInterval.Minute, CDate(RelojNow), CDate(Reloj24h)) / 60))  'Obtiene las horas
       m = CInt(DateDiff(DateInterval.Minute, CDate(RelojNow), CDate(Reloj24h)) - (h * 60)) 'Obtiene los minutos
       s = CInt(DateDiff(DateInterval.Second, CDate(RelojNow), CDate(Reloj24h)) - ((h * 3600) + (m * 60)))
 
       'Resta un segundo
       If h < 0 Then h = h * (-1)
       If m < 0 Then m = m * (-1)
       If s < 0 Then s = s * (-1)
       CronoTime = CDate((h & ":" & m & ":" & s).ToString)
 
       'Si el cronómentro llega a cero resta un día
       If Hour(CronoTime) = 0 And Minute(CronoTime) = 0 And Second(CronoTime) = 0 Then DiaMenos -= 1
 
       CronoFecha = DateAdd(DateInterval.Day, DiaMenos, CDate(FechaLim)) 'Obtiene una fecha resultado de restar x días a la fecha límite
 
       'Convierte las fechas a DATE y obtiene la diferencia de días
       Dim datTim1 As Date = CDate(FechaLim) : Dim datTim2 As Date = CDate(CronoFecha)
       DiasRestantes = CInt(TotalDias - DateAndTime.DateDiff(DateInterval.Day, CDate(datTim2), CDate(datTim1)))
 
       Label1.Text = _
       "Fecha Actual: " & Strings.FormatDateTime(Date.Now, DateFormat.LongDate) & ControlChars.CrLf & _
       "Fecha Límite: " & Strings.FormatDateTime(CDate(Replace(CStr(CDate(FechaLim)), "#", "", 1)), DateFormat.LongDate) & ControlChars.CrLf &
       "Quedan: " & DiasRestantes & " días " & "y " & Strings.Format(CronoTime, "HH:mm:ss")
 
   End Sub
End Class
 

más info:
https://msdn.microsoft.com/es-es/library/b5xbyt6f%28v=vs.90%29.aspx

Saludos



Otra cosa, si quieres saber cuantos días tiene un mes de un determinado año usa:

Código
'devuelve 28
Date.DaysInMonth(2015, 2)


Navegación

[0] Índice de Mensajes

[#] Página Siguiente