153 lines
6.4 KiB
VB.net
153 lines
6.4 KiB
VB.net
Imports System.Runtime.CompilerServices
|
|
|
|
Namespace Extensiones
|
|
Public Module DateTimeExtensions
|
|
<Extension()>
|
|
Public Function FechaNulableAString(Fecha As Date?) As String
|
|
If Fecha Is Nothing Then
|
|
Return ""
|
|
Else
|
|
Return Fecha.Value.ToString("dd/MM/yyyy")
|
|
End If
|
|
End Function
|
|
|
|
<Extension()>
|
|
Public Function FechaHoraStringADate(Fecha As String, Optional SinSegundos As Boolean = False) As DateTime?
|
|
Dim dt As New DateTime
|
|
If Fecha = "0" Then
|
|
Return Nothing
|
|
Else
|
|
If Fecha.Contains("_") Then
|
|
Dim s() As String = Fecha.Split("_")
|
|
dt = New DateTime(s(0), s(1), s(2), s(3), s(4), s(5))
|
|
Else
|
|
If Fecha.Contains(".") And (Fecha.Length = 13 OrElse Fecha.Length = 12) Then
|
|
Fecha = Fecha.Split(".")(0) & Fecha.Split(".")(1).Substring(0, 2) & Math.Round(Double.Parse(Fecha.Split(".")(1).Substring(2)) * 60 / 100, 0, MidpointRounding.AwayFromZero).ToString.PadLeft(2, "0") & "00"
|
|
dt = New DateTime(Fecha.Substring(0, 4), Fecha.Substring(4, 2), Fecha.Substring(6, 2), Fecha.Substring(8, 2), Fecha.Substring(10, 2), Fecha.Substring(12, 2))
|
|
Else
|
|
If Fecha.Length = 14 Then
|
|
dt = New DateTime(Fecha.Substring(0, 4), Fecha.Substring(4, 2), Fecha.Substring(6, 2), Fecha.Substring(8, 2), Fecha.Substring(10, 2), Fecha.Substring(12, 2))
|
|
Else
|
|
If Fecha.Length = 19 Then
|
|
dt = New DateTime(Fecha.Substring(0, 4), Fecha.Substring(5, 2), Fecha.Substring(8, 2), Fecha.Substring(11, 2), Fecha.Substring(14, 2), Fecha.Substring(17, 2))
|
|
Else
|
|
If Fecha.Length = 6 Then Fecha = "19" & Fecha
|
|
If Fecha.Contains(".") Then
|
|
Dim horas = Double.Parse(Fecha.Split(".")(1).PadRight(6, "0")) / 10000
|
|
Dim Segundos = horas * 60 * 60
|
|
Dim ts = TimeSpan.FromSeconds(Segundos)
|
|
dt = New DateTime(Integer.Parse(Fecha.Substring(0, 4)), Integer.Parse(Fecha.Substring(4, 2)), Integer.Parse(Fecha.Substring(6, 2)))
|
|
dt = dt + ts
|
|
Else
|
|
dt = New Date(Integer.Parse(Fecha.Substring(0, 4)), Integer.Parse(Fecha.Substring(4, 2)), Integer.Parse(Fecha.Substring(6, 2)))
|
|
End If
|
|
End If
|
|
End If
|
|
End If
|
|
End If
|
|
If SinSegundos Then dt = New Date(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, 0)
|
|
Return dt
|
|
End If
|
|
End Function
|
|
|
|
|
|
|
|
<Extension()>
|
|
Public Function Maximo(Fecha1 As Nullable(Of DateTime), Fecha2 As Nullable(Of DateTime)) As Nullable(Of DateTime)
|
|
Dim t1, t2 As Long
|
|
If Fecha1.HasValue Then t1 = Fecha1.Value.Ticks
|
|
If Fecha2.HasValue Then t2 = Fecha2.Value.Ticks
|
|
If t1 > t2 Then
|
|
Return Fecha1
|
|
Else
|
|
Return Fecha2
|
|
End If
|
|
End Function
|
|
<Extension()>
|
|
Public Function Minimo(Fecha1 As Nullable(Of DateTime), Fecha2 As Nullable(Of DateTime)) As Nullable(Of DateTime)
|
|
Dim t1, t2 As Long
|
|
If Fecha1.HasValue Then t1 = Fecha1.Value.Ticks
|
|
If Fecha2.HasValue Then t2 = Fecha2.Value.Ticks
|
|
If t1 < t2 Then
|
|
Return Fecha1
|
|
Else
|
|
Return Fecha2
|
|
End If
|
|
End Function
|
|
<Extension()>
|
|
Public Function MesCastellano(Fecha As Date) As String
|
|
Select Case Fecha.Month
|
|
Case 1
|
|
Return "Enero"
|
|
Case 2
|
|
Return "Febrero"
|
|
Case 3
|
|
Return "Marzo"
|
|
Case 4
|
|
Return "Abril"
|
|
Case 5
|
|
Return "Mayo"
|
|
Case 6
|
|
Return "Junio"
|
|
Case 7
|
|
Return "Julio"
|
|
Case 8
|
|
Return "Agosto"
|
|
Case 9
|
|
Return "Septiembre"
|
|
Case 10
|
|
Return "Octubre"
|
|
Case 11
|
|
Return "Noviembre"
|
|
Case Else
|
|
Return "Diciembre"
|
|
|
|
End Select
|
|
End Function
|
|
<Extension>
|
|
Public Function ValorNumerico(Fecha As Date) As Long
|
|
Return Fecha.Year * 10000 + Fecha.Month * 100 + Fecha.Day
|
|
End Function
|
|
<Extension>
|
|
Public Function ValorNumerico(Fecha As Nullable(Of Date)) As Long
|
|
If Fecha Is Nothing Then
|
|
Return 0
|
|
Else
|
|
Return Fecha.Value.Year * 10000 + Fecha.Value.Month * 100 + Fecha.Value.Day
|
|
End If
|
|
End Function
|
|
'Public Function Maximo(Fecha1 As DateTime, Fecha2 As DateTime) As DateTime
|
|
' Dim t1, t2 As Long
|
|
' If Fecha1 Is Nothing Then t1 = Fecha1.Ticks
|
|
' If Not Fecha2 Is Nothing Then t2 = Fecha2.Ticks
|
|
' If t1 > t2 Then
|
|
' Return Fecha1
|
|
' Else
|
|
' Return Fecha2
|
|
' End If
|
|
'End Function
|
|
|
|
'Public Function Minimo(Fecha1 As Nullable(Of DateTime), Fecha2 As Nullable(Of DateTime)) As DateTime
|
|
' Dim t1, t2 As Long
|
|
' If Fecha1.HasValue Then t1 = Fecha1.Value.Ticks
|
|
' If Fecha2.HasValue Then t2 = Fecha2.Value.Ticks
|
|
' If t1 < t2 Then
|
|
' Return Fecha1
|
|
' Else
|
|
' Return Fecha2
|
|
' End If
|
|
'End Function
|
|
|
|
'Public Function Maximo(Fecha1 As Nullable(Of Date), Fecha2 As Nullable(Of Date)) As Date
|
|
' Dim t1, t2 As Long
|
|
' If Fecha1.HasValue Then t1 = Fecha1.Value.Ticks
|
|
' If Fecha2.HasValue Then t2 = Fecha2.Value.Ticks
|
|
' If t1 > t2 Then
|
|
' Return Fecha1
|
|
' Else
|
|
' Return Fecha2
|
|
' End If
|
|
'End Function
|
|
End Module
|
|
End Namespace
|