Sunday, January 31, 2016

Date format - conversions in vbscript

We have always challenges with date format conversions in QTP/UFT because vbscript supports minimal date functions. of course, we cause use dotnetfactory and few advanced concepts on date related formats/methods/conversions.

Here, a simple function which can be understandable to everyone who have basic idea of vbscript.

you can add your required condition in below function and utilize.

'/* Function Name: formatDate
'/* Function Description: function to convert input date format to user desired format and return with expected delimeter
'/* Function Inputs: inputDateAsSysFormat (send an input date with same system format), outputDateFormat (output, you required with format), delimeter (date, month, year which should separate with)
'/* Function Returns: expected outputDateFormat
Function formatDate(inputDateAsSysFormat, outputDateformat, delimeter)
    Dim actDate
   
    Select Case outputDateFormat
        Case "MDYYYY""mdyyyy"
            actDate = FormatDateTime(inputDateAsSysFormat,2)
            mon = Month(actDate)
            yr = Year(actDate)
            dayy = Day(actDate)
            formatDate = mon&delimeter&dayy&delimeter&yr
        Case "DMYYYY""dmyyyy"
            actDate = FormatDateTime(inputDateAsSysFormat,2)
            mon = Month(actDate)
            yr = Year(actDate)
            dayy = Day(actDate)
            formatDate = dayy&delimeter&mon&delimeter&yr
        Case "MDYY""mdyy"
            actDate = FormatDateTime(inputDateAsSysFormat,2)
            mon = Month(actDate)
            yr = Year(actDate)
            dayy = Day(actDate)
            formatDate = mon&delimeter&dayy&delimeter&right(yr, 2)
        Case "DMYY""dmyy"
            actDate = FormatDateTime(inputDateAsSysFormat,2)
            mon = Month(actDate)
            yr = Year(actDate)
            dayy = Day(actDate)
            formatDate = dayy&delimeter&mon&delimeter&right(yr, 2)
        Case "MMDDYY""mmddyy"
            actDate = FormatDateTime(inputDateAsSysFormat,2)
            mon = Month(actDate)
            If len(mon)=1 Then
                mon = "0"&mon
            End If
            yr = Year(actDate)
            dayy = Day(actDate)
            If len(dayy)=1 Then
                dayy = "0"&dayy
            End If
            formatDate = mon&delimeter&dayy&delimeter&right(yr, 2)
        Case "MMDDYYYY",  "mmddyyyy"
            actDate = FormatDateTime(inputDateAsSysFormat,2)
            mon = Month(actDate)
            If len(mon)=1 Then
                mon = "0"&mon
            End If
            yr = Year(actDate)
            dayy = Day(actDate)
            If len(dayy)=1 Then
                dayy = "0"&dayy
            End If
            formatDate = mon&delimeter&dayy&delimeter&yr
        Case "MMMDDYYYY""mmmddyyyy"
            actDate = FormatDateTime(inputDateAsSysFormat,1)
            mon = left(Trim(Split(actDate, ",")(1)), 3)
            yr = Trim(split(actDate, ",")(2))
            dayy = Trim(Split(Trim(Split(actDate, ",")(1)), " ")(1))
            If len(dayy)=1 Then
                dayy = "0"&dayy
            End If
            formatDate = mon&delimeter&dayy&delimeter&yr
        Case "MMMDYYYY""mmmdyyyy"
            actDate = FormatDateTime(inputDateAsSysFormat,1)
            mon = left(Trim(Split(actDate, ",")(1)), 3)
            yr = Trim(split(actDate, ",")(2))
            dayy = Trim(Split(Trim(Split(actDate, ",")(1)), " ")(1))
            If len(dayy)=2 Then
                dayy = right(dayy, 1)
            End If
            formatDate = mon&delimeter&dayy&delimeter&yr
        Case "MMMDDYY""mmmddyy"
            actDate = FormatDateTime(inputDateAsSysFormat,1)
            mon = left(Trim(Split(actDate, ",")(1)), 3)
            yr = Trim(split(actDate, ",")(2))
            dayy = Trim(Split(Trim(Split(actDate, ",")(1)), " ")(1))
            formatDate = mon&delimeter&dayy&delimeter&right(yr, 2)
        Case "MMMDYY""mmmdyy"
            actDate = FormatDateTime(inputDateAsSysFormat,1)
            mon = left(Trim(Split(actDate, ",")(1)), 3)
            yr = Trim(split(actDate, ",")(2))
            dayy = Trim(Split(Trim(Split(actDate, ",")(1)), " ")(1))
            If len(dayy)=2 Then
                dayy = right(dayy, 1)
            End If
            formatDate = mon&delimeter&dayy&delimeter&right(yr, 2)
        Case "DDMMYYYY""ddmmyyyy"
            actDate = FormatDateTime(inputDateAsSysFormat,2)
            mon = Month(actDate)
            If len(mon)=1 Then
                mon = "0"&mon
            End If
            yr = Year(actDate)
            dayy = Day(actDate)
            If len(dayy)=1 Then
                dayy = "0"&dayy
            End If
            formatDate = dayy&delimeter&mon&delimeter&yr
        Case "DDMMYY",  "ddmmyy"
            actDate = FormatDateTime(inputDateAsSysFormat,2)
            mon = Month(actDate)
            If len(mon)=1 Then
                mon = "0"&mon
            End If
            yr = Year(actDate)
            dayy = Day(actDate)
            If len(dayy)=1 Then
                dayy = "0"&dayy
            End If
            formatDate = dayy&delimeter&mon&delimeter&right(yr, 2)
        Case "DDMMMYY""ddmmmyy"
            actDate = FormatDateTime(inputDateAsSysFormat,1)
            mon = left(Trim(Split(actDate, ",")(1)), 3)
            yr = Trim(split(actDate, ",")(2))
            dayy = Trim(Split(Trim(Split(actDate, ",")(1)), " ")(1))
            If len(dayy)=1 Then
                dayy = "0"&dayy
            End If
            formatDate = dayy&delimeter&mon&delimeter&right(yr,2)
        Case "DMMMYY""dmmmyy"
            actDate = FormatDateTime(inputDateAsSysFormat,1)
            mon = left(Trim(Split(actDate, ",")(1)), 3)
            yr = Trim(split(actDate, ",")(2))
            dayy = Trim(Split(Trim(Split(actDate, ",")(1)), " ")(1))
            If len(dayy)=2 Then
                dayy = right(dayy, 1)
            End If
            formatDate = dayy&delimeter&mon&delimeter&right(yr, 2)
        Case "DDMMMYYYY""ddmmmyyyy"
            actDate = FormatDateTime(inputDateAsSysFormat,1)
            mon = left(Trim(Split(actDate, ",")(1)), 3)
            yr = Trim(split(actDate, ",")(2))
            dayy = Trim(Split(Trim(Split(actDate, ",")(1)), " ")(1))
            If len(dayy)=1 Then
                dayy = "0"&dayy
            End If
            formatDate = dayy&delimeter&mon&delimeter&yr
        Case "DMMMYYYY""dmmmyyyy"
            actDate = FormatDateTime(inputDateAsSysFormat,1)
            mon = left(Trim(Split(actDate, ",")(1)), 3)
            yr = Trim(split(actDate, ",")(2))
            dayy = Trim(Split(Trim(Split(actDate, ",")(1)), " ")(1))
            If len(dayy)=2 Then
                dayy = right(dayy, 1)
            End If
            formatDate = dayy&delimeter&mon&delimeter&yr
        Case "YYMMDD""yymmdd"
            actDate = FormatDateTime(inputDateAsSysFormat,2)
            mon = Month(actDate)
            If len(mon)=1 Then
                mon = "0"&mon
            End If
            yr = Year(actDate)
            dayy = Day(actDate)
            If len(dayy)=1 Then
                dayy = "0"&dayy
            End If
            formatDate = right(yr, 2)&delimeter&mon&delimeter&dayy
        Case "YYYYMMDD""yyyymmdd"
            actDate = FormatDateTime(inputDateAsSysFormat,2)
            mon = Month(actDate)
            If len(mon)=1 Then
                mon = "0"&mon
            End If
            yr = Year(actDate)
            dayy = Day(actDate)
            If len(dayy)=1 Then
                dayy = "0"&dayy
            End If
            formatDate = yr&delimeter&mon&delimeter&dayy
        Case "YYMMMDD""yymmmdd"
            actDate = FormatDateTime(inputDateAsSysFormat,1)
            mon = left(Trim(Split(actDate, ",")(1)), 3)
            yr = Trim(split(actDate, ",")(2))
            dayy = Trim(Split(Trim(Split(actDate, ",")(1)), " ")(1))
            If len(dayy)=1 Then
                dayy = "0"&dayy
            End If
            formatDate = right(yr, 2)&delimeter&mon&delimeter&dayy
        Case "YYMMMD""yymmmd"
            actDate = FormatDateTime(inputDateAsSysFormat,1)
            mon = left(Trim(Split(actDate, ",")(1)), 3)
            yr = Trim(split(actDate, ",")(2))
            dayy = Trim(Split(Trim(Split(actDate, ",")(1)), " ")(1))
            If len(dayy)=2 Then
                dayy = right(dayy, 1)
            End If
            formatDate = right(yr, 2)&delimeter&mon&delimeter&dayy
        Case "YYYYMMMDD""yyyymmmdd"
            actDate = FormatDateTime(inputDateAsSysFormat,1)
            mon = left(Trim(Split(actDate, ",")(1)), 3)
            yr = Trim(split(actDate, ",")(2))
            dayy = Trim(Split(Trim(Split(actDate, ",")(1)), " ")(1))
            If len(dayy)=1 Then
                dayy = "0"&dayy
            End If
            formatDate = yr&delimeter&mon&delimeter&day
        Case "YYYYMMMD""yyyymmmd"
            actDate = FormatDateTime(inputDateAsSysFormat,1)
            mon = left(Trim(Split(actDate, ",")(1)), 3)
            yr = Trim(split(actDate, ",")(2))
            dayy = Trim(Split(Trim(Split(actDate, ",")(1)), " ")(1))
            If len(dayy)=2 Then
                dayy = right(dayy, 1)
            End If
            formatDate = yr&delimeter&mon&delimeter&day
        Case else
            Print "output Format expected is not set in this function"
            formatDate = Null
    End Select
End Function

Share your comments if you've any. Thank you - Baba

"A good threat is worth a thousand tests" - Boris Beizer

No comments:

There was an error in this gadget