Skip to main content

VB.NET Date Format Utility Class

Date Formating Utility consists of various functions from parsing date to date validation.
Imports System.Globalization
Public Class DateUtils
Public Const DEFAULT_DATE_FORMAT As String = "dd/MM/yyyy"
''' <summary>
''' Converts String date to System.DateTime based on default local.
''' </summary>
''' <param name="data"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ParseDateTime(ByVal data As String) As DateTime
Dim obj As DateTime = DateTime.Parse(data, CultureInfo.InvariantCulture)
Return obj
End Function
''' <summary>
'''
''' </summary>
''' <param name="data"></param>
''' <param name="dateFormat"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ParseDateTime(ByVal data As String, ByVal dateFormat As String) As DateTime
Dim obj As DateTime = Nothing
If Not String.IsNullOrEmpty(dateFormat) Then
obj = DateTime.ParseExact(data, dateFormat, CultureInfo.InvariantCulture)
Else
obj = DateTime.ParseExact(data, DateUtils.DEFAULT_DATE_FORMAT, CultureInfo.InvariantCulture)
End If
Return obj
End Function
''' <summary>
''' Checks if date with dateFormat is parseable to System.DateTime format returns boolean value if true else false
''' </summary>
''' <param name="data">String date</param>
''' <param name="dateFormat">date format example dd/MM/yyyy HH:mm:ss</param>
''' <returns>boolean True False if is valid System.DateTime</returns>
''' <remarks></remarks>
Public Shared Function IsDateTime(ByVal data As String, ByVal dateFormat As String) As Boolean
Dim dateVal As DateTime
If DateTime.TryParseExact(data, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, dateVal) Then
If Year(dateVal) < 1946 Then
Return False
End If
Return True
Else
Return False
End If
End Function
''' <summary>
'''
''' </summary>
''' <param name="data"></param>
''' <param name="dateFormat"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function FormatDate(ByVal data As DateTime, ByVal dateFormat As String) As String
Return data.ToString(dateFormat, DateTimeFormatInfo.InvariantInfo)
End Function
''' <summary>
''' Gets current Date time. expressed as local time format.
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetCurrentDate() As DateTime
Return DateTime.Now
End Function
''' <summary>
''' converts a long representation of DateTime into a string format HH:mm:ss.fff
''' Function may be used for testing batch processes performance.
''' </summary>
''' <param name="time"></param>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function ConvertMillisecToString(ByVal time As Long) As String
Dim milliseconds As Integer = CInt(Fix(time Mod 1000))
Dim seconds As Integer = CInt(Fix((time \ 1000) Mod 60))
Dim minutes As Integer = CInt(Fix((time \ 60000) Mod 60))
Dim hours As Integer = CInt(Fix((time \ 3600000) Mod 24))
Dim millisecondsStr As String = (If(milliseconds < 10, "00", (If(milliseconds < 100, "0", "")))) + milliseconds.ToString
Dim secondsStr As String = (If(seconds < 10, "0", "")) + seconds.ToString
Dim minutesStr As String = (If(minutes < 10, "0", "")) + minutes.ToString
Dim hoursStr As String = (If(hours < 10, "0", "")) + hours.ToString
Return New String(hoursStr + ":" + minutesStr + ":" + secondsStr + "." + millisecondsStr)
End Function
Public Shared Function dateDiff(ByVal startTime As Date, ByVal endTime As Date) As Integer
'Dim date1 As New System.DateTime(1996, 6, 3, 22, 15, 0)
'Dim date2 As New System.DateTime(1996, 12, 6, 13, 2, 0)
Dim diff As System.TimeSpan
diff = endTime.Subtract(startTime)
Return diff.Hours
End Function
Public Shared Function dayDiff(ByVal startTime As Date, ByVal endTime As Date) As Integer
'Dim date1 As New System.DateTime(1996, 6, 3, 22, 15, 0)
'Dim date2 As New System.DateTime(1996, 12, 6, 13, 2, 0)
Dim diff As System.TimeSpan
diff = endTime.Subtract(startTime)
Return diff.Days
End Function
''' <summary>
''' Gets todays date + 1 day tomorrows date
''' </summary>
''' <returns></returns>
''' <remarks></remarks>
Public Shared Function GetTomorrowDate() As Date
Return Date.Now.AddDays(1)
End Function
End Class
view raw DateUtil.vb hosted with ❤ by GitHub

Comments

Popular posts from this blog

Building your first django app in minutes (Conjure up that overdue MVP)

As the trends shift towards data science and big data, machine learning there’s has never been a more lucrative time to start learning python and Django. Building your first Django app in minutes course takes an in-depth look at Django for web development with python. The course is tailored for both beginners, experts, to be founders.  Why not start today learning so you can land yourself the next dream job, build your MVP fast, get that work, and school project done fast. Coding doesn’t have to be mystic, it’s actually more familiar than you think.   Checkout this awesome  75% limited time discount