Sometimes we need to set the date format in various formats depending on the client requirements.
This article describes doing the same thing, except you have to send the format as a parameter; the result we get is in the same format as that we sent.
Let's see with sample code.
Pass two parameters to function.
Dim todate As Date
Dim fromdate As Date
fromdate = ClsDate.GetFormatedDate(TextBox1.Text, "DD/MM/YYYY")
todate = ClsDate.GetFormatedDate(TextBox2.Text, "DD/MM/YYYY")
We will get the result in "todate" and "fromdate" date variables.
//Our class
Public Class ClsDate
Shared Function GetFormatedDate(ByVal strdate As String, ByVal format As String) As String
Dim Splitstrdate As String()
If strdate.Trim <> "" Then
Select Case format
Case "YYYYMMDD"
GetFormatedDate = GetMonth(strdate.Substring(4, 2)) & " " & strdate.Substring(6, 2) & " " & strdate.Substring(0, 4)
Case "DDMMYYYY"
GetFormatedDate = GetMonth(strdate.Substring(2, 2)) & " " & strdate.Substring(0, 2) & " " & strdate.Substring(4, 4)
Case "DD-MM-YYYY"
Splitstrdate = Split(strdate, "-")
GetFormatedDate = GetMonth(Splitstrdate(1)) & " " & Splitstrdate(0) & " " & Splitstrdate(2)
Case "DD/MM/YYYY"
Splitstrdate = Split(strdate, "/")
If Left(Splitstrdate(0), 1) = "0" Then
GetFormatedDate = GetMonth(Splitstrdate(1)) & " " & Right(Splitstrdate(0), 1) & " " & Splitstrdate(2)
Else
GetFormatedDate = GetMonth(Splitstrdate(1)) & " " & Splitstrdate(0) & " " & Splitstrdate(2)
End If
End Select
Else
GetFormatedDate = ""
End If
End Function
'for getting month
Shared Function GetMonth(ByVal strmonth As String) As String
Select Case strmonth
Case "1", "01"
GetMonth = "Jan"
Case "2", "02"
GetMonth = "Feb"
Case "3", "03"
GetMonth = "Mar"
Case "4", "04"
GetMonth = "Apr"
Case "5", "05"
GetMonth = "May"
Case "6", "06"
GetMonth = "Jun"
Case "7", "07"
GetMonth = "Jul"
Case "8", "08"
GetMonth = "Aug"
Case "9", "09"
GetMonth = "Sep"
Case "10"
GetMonth = "Oct"
Case "11"
GetMonth = "Nov"
Case "12"
GetMonth = "Dec"
End Select
End Function