lunes, mayo 16, 2011

Intervalo de Tiempo Intervalo de Tiempo Desarrollado en Visual Basic.Net

Introducción

Casi siempre tenemos problemas cuando queremos trabajar ya sea con fechas u horas, creo yo que más de uno, nos hemos roto la cabeza tratando de solucionar este tipo de inconveniente, por ello he tratado de desarrollar este pequeño pero ilustrativo artículo donde muestro como podemos trabajar con estos tipos de datos en Visual Basic .Net.

Primero vamos recordar que dentro de SYSTEM (Espacio de Nombres) podemos localizar una serie de miembros que pueden ser Clases, Interfaces, Estructuras, Delegados y Enumeraciones que nos permiten realizar una serie de tareas, pero de quien nos vamos a centrar en este artículo es TIMESPAN (Estructura).

TimeSpan (Estructura)

Nos permite representar un intervalo de tiempo tomando como base una Fecha/Hora Inicial y una Fecha/Hora Final, devuelve la cantidad de Días, Horas, Minutos, Segundos, etc., transcurridos entre este intervalo de tiempo.

Hay que recordar la siguiente jerarquía.

System

System.Object

System.ValueType

System.TimeSpan

Estos son los requisitos para poder utilizarlo:

Espacio de Nombres: System
Plataformas: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional Edition, Familia de Windows Server 2003, .NET Compact Framework - Windows CE .NET

Ensamblado: Mscorlib (en Mscorlib.dll)

....Bueno ahora vamos ha realizar lo que más nos agrada, que es un ejemplo de como hallar el intervalo de tiempo transcurrido entre dos fechas/horas:

Primero; realizamos lo que conocemos como Tiempo de Diseño:

Interfaz: Debemos de crear un formulario con los siguientes controles.

....Ahora vamos a ver como debe de quedar el Explorador de Soluciones:

Segundo; ahora vamos a tratar de describir lo que llamamos el Tiempo de Ejecución:

Public Class FrmInterDateTime
Inherits System.Windows.Forms.Form
Public flagestado As Boolean = False

#Region " Código generado por el Diseñador de Windows Forms "

Public Sub New()
MyBase.New()

'El Diseñador de Windows Forms requiere esta llamada.
InitializeComponent()

'Agregar cualquier inicialización después de la llamada a InitializeComponent()

End Sub

'Form reemplaza a Dispose para limpiar la lista de componentes.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Requerido por el Diseñador de Windows Forms
Private components As System.ComponentModel.IContainer

'NOTA: el Diseñador de Windows Forms requiere el siguiente procedimiento
'Puede modificarse utilizando el Diseñador de Windows Forms.
'No lo modifique con el editor de código.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents Label7 As System.Windows.Forms.Label
Friend WithEvents TxtDTInicio As System.Windows.Forms.TextBox
Friend WithEvents TxtDtFinal As System.Windows.Forms.TextBox
Friend WithEvents TxtDia As System.Windows.Forms.TextBox
Friend WithEvents TxtHra As System.Windows.Forms.TextBox
Friend WithEvents TxtMin As System.Windows.Forms.TextBox
Friend WithEvents TxtSeg As System.Windows.Forms.TextBox
Friend WithEvents TimerTitulo As System.Windows.Forms.Timer
Friend WithEvents TimerOpacity As System.Windows.Forms.Timer
Friend WithEvents TimerOpacity2 As System.Windows.Forms.Timer
Friend WithEvents CmdIntervalo As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(FrmInterDateTime))
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.Label3 = New System.Windows.Forms.Label
Me.Label4 = New System.Windows.Forms.Label
Me.Label5 = New System.Windows.Forms.Label
Me.Label6 = New System.Windows.Forms.Label
Me.Label7 = New System.Windows.Forms.Label
Me.TxtDTInicio = New System.Windows.Forms.TextBox
Me.TxtDtFinal = New System.Windows.Forms.TextBox
Me.CmdIntervalo = New System.Windows.Forms.Button
Me.TxtDia = New System.Windows.Forms.TextBox
Me.TxtHra = New System.Windows.Forms.TextBox
Me.TxtMin = New System.Windows.Forms.TextBox
Me.TxtSeg = New System.Windows.Forms.TextBox
Me.TimerTitulo = New System.Windows.Forms.Timer(Me.components)
Me.TimerOpacity = New System.Windows.Forms.Timer(Me.components)
Me.TimerOpacity2 = New System.Windows.Forms.Timer(Me.components)
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 16.0!, CType((System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Underline), System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.ForeColor = System.Drawing.Color.RoyalBlue
Me.Label1.Location = New System.Drawing.Point(63, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(210, 28)
Me.Label1.TabIndex = 8
Me.Label1.Text = "Intervalo de Tiempo"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.ForeColor = System.Drawing.Color.Crimson
Me.Label2.Location = New System.Drawing.Point(16, 48)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(96, 16)
Me.Label2.TabIndex = 1
Me.Label2.Text = "Fecha/Hora Inicial"
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.ForeColor = System.Drawing.Color.Crimson
Me.Label3.Location = New System.Drawing.Point(176, 48)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(91, 16)
Me.Label3.TabIndex = 2
Me.Label3.Text = "Fecha/Hora Final"
'
'Label4
'
Me.Label4.AutoSize = True
Me.Label4.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label4.ForeColor = System.Drawing.Color.Teal
Me.Label4.Location = New System.Drawing.Point(61, 96)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(31, 16)
Me.Label4.TabIndex = 3
Me.Label4.Text = "Días:"
'
'Label5
'
Me.Label5.AutoSize = True
Me.Label5.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label5.ForeColor = System.Drawing.Color.Teal
Me.Label5.Location = New System.Drawing.Point(53, 120)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(39, 16)
Me.Label5.TabIndex = 4
Me.Label5.Text = "Horas:"
'
'Label6
'
Me.Label6.AutoSize = True
Me.Label6.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label6.ForeColor = System.Drawing.Color.Teal
Me.Label6.Location = New System.Drawing.Point(43, 144)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(49, 16)
Me.Label6.TabIndex = 5
Me.Label6.Text = "Minutos:"
'
'Label7
'
Me.Label7.AutoSize = True
Me.Label7.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label7.ForeColor = System.Drawing.Color.Teal
Me.Label7.Location = New System.Drawing.Point(32, 168)
Me.Label7.Name = "Label7"
Me.Label7.Size = New System.Drawing.Size(60, 16)
Me.Label7.TabIndex = 6
Me.Label7.Text = "Segundos:"
'
'TxtDTInicio
'
Me.TxtDTInicio.Location = New System.Drawing.Point(16, 64)
Me.TxtDTInicio.Name = "TxtDTInicio"
Me.TxtDTInicio.Size = New System.Drawing.Size(144, 20)
Me.TxtDTInicio.TabIndex = 1
Me.TxtDTInicio.Text = "08/07/2004 08:00:00 a.m."
'
'TxtDtFinal
'
Me.TxtDtFinal.Location = New System.Drawing.Point(176, 64)
Me.TxtDtFinal.Name = "TxtDtFinal"
Me.TxtDtFinal.Size = New System.Drawing.Size(144, 20)
Me.TxtDtFinal.TabIndex = 2
Me.TxtDtFinal.Text = ""
'
'CmdIntervalo
'
Me.CmdIntervalo.ForeColor = System.Drawing.Color.Navy
Me.CmdIntervalo.Image = CType(resources.GetObject("CmdIntervalo.Image"), System.Drawing.Image)
Me.CmdIntervalo.ImageAlign = System.Drawing.ContentAlignment.TopCenter
Me.CmdIntervalo.Location = New System.Drawing.Point(208, 112)
Me.CmdIntervalo.Name = "CmdIntervalo"
Me.CmdIntervalo.Size = New System.Drawing.Size(72, 56)
Me.CmdIntervalo.TabIndex = 0
Me.CmdIntervalo.Text = "&Hallar Intervalo"
Me.CmdIntervalo.TextAlign = System.Drawing.ContentAlignment.BottomCenter
'
'TxtDia
'
Me.TxtDia.Location = New System.Drawing.Point(96, 96)
Me.TxtDia.Name = "TxtDia"
Me.TxtDia.ReadOnly = True
Me.TxtDia.Size = New System.Drawing.Size(60, 20)
Me.TxtDia.TabIndex = 9
Me.TxtDia.Text = ""
Me.TxtDia.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
'
'TxtHra
'
Me.TxtHra.Location = New System.Drawing.Point(96, 120)
Me.TxtHra.Name = "TxtHra"
Me.TxtHra.ReadOnly = True
Me.TxtHra.Size = New System.Drawing.Size(60, 20)
Me.TxtHra.TabIndex = 10
Me.TxtHra.Text = ""
Me.TxtHra.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
'
'TxtMin
'
Me.TxtMin.Location = New System.Drawing.Point(96, 144)
Me.TxtMin.Name = "TxtMin"
Me.TxtMin.ReadOnly = True
Me.TxtMin.Size = New System.Drawing.Size(60, 20)
Me.TxtMin.TabIndex = 11
Me.TxtMin.Text = ""
Me.TxtMin.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
'
'TxtSeg
'
Me.TxtSeg.Location = New System.Drawing.Point(96, 168)
Me.TxtSeg.Name = "TxtSeg"
Me.TxtSeg.ReadOnly = True
Me.TxtSeg.Size = New System.Drawing.Size(60, 20)
Me.TxtSeg.TabIndex = 12
Me.TxtSeg.Text = ""
Me.TxtSeg.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
'
'TimerTitulo
'
Me.TimerTitulo.Enabled = True
Me.TimerTitulo.Interval = 300
'
'TimerOpacity
'
'
'TimerOpacity2
'
Me.TimerOpacity2.Enabled = True
'
'FrmInterDateTime
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(336, 200)
Me.Controls.Add(Me.TxtSeg)
Me.Controls.Add(Me.TxtMin)
Me.Controls.Add(Me.TxtHra)
Me.Controls.Add(Me.TxtDia)
Me.Controls.Add(Me.CmdIntervalo)
Me.Controls.Add(Me.TxtDtFinal)
Me.Controls.Add(Me.TxtDTInicio)
Me.Controls.Add(Me.Label7)
Me.Controls.Add(Me.Label6)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.MaximizeBox = False
Me.Name = "FrmInterDateTime"
Me.Opacity = 0
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Intervalo de Tiempo - alextaya@hotmail.com "
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub FrmInterDateTime_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TxtDtFinal.Text = Now
End Sub

Private Sub VerInterDateTime(ByVal TSpan As TimeSpan)
TxtDia.Text = TSpan.Days.ToString
TxtHra.Text = TSpan.Hours.ToString
TxtMin.Text = TSpan.Minutes.ToString
TxtSeg.Text = TSpan.Seconds.ToString
End Sub

Private Sub TimerTitulo_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerTitulo.Tick
Me.Text = Microsoft.VisualBasic.Right(Me.Text, (Len(Me.Text) - 1)) + Microsoft.VisualBasic.Left(Me.Text, 1)
End Sub

Private Sub TimerOpacity_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerOpacity.Tick
Me.Opacity -= 0.05
If Me.Opacity = 0 Then
flagestado = True
Me.Close()
End If
End Sub

Private Sub FrmInterDateTime_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If flagestado = False Then
e.Cancel = True
Me.TimerOpacity.Enabled = True
End If
End Sub

Private Sub TimerOpacity2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimerOpacity2.Tick
Me.Opacity += 0.05
If Me.Opacity = 1 Then
Me.TimerOpacity2.Enabled = False
End If
End Sub

Private Sub CmdIntervalo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdIntervalo.Click
Dim TSpan As TimeSpan
Dim DTInicio As DateTime
Dim DtFinal As DateTime

DTInicio = DateTime.Parse(TxtDTInicio.Text)
DtFinal = DateTime.Parse(TxtDtFinal.Text)

TSpan = DtFinal.Subtract(DTInicio).Duration
VerInterDateTime(TSpan)
End Sub
End Class

Ahora simplemente nos queda ejecutar la solución para ver que todo este bien, espero que funcione
sin ningún problema jeje ;).



Espero poder publicar un artículo similar pero utilizando C# en los próximos días.



Me despido esperando haber contribuido con un granito de arena en este fascinante mundo .Net. Nos vemos líneas de código más adelante.

No hay comentarios.:

¿Qué es un Algoritmo?

Más información ==>  https://m.facebook.com/story.php?story_fbid=779713015547334&id=332220556963251