Some Times we need to to set date format in various formats as per client need.
Article describes same, only you have to send format as parameter, result we get
in same format in which we sent.
Lets 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 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