Micro Focus QTP (UFT) Forums
DateFormat help - Printable Version

+- Micro Focus QTP (UFT) Forums (https://www.learnqtp.com/forums)
+-- Forum: Micro Focus UFT (earlier known as QTP) (https://www.learnqtp.com/forums/Forum-Micro-Focus-UFT-earlier-known-as-QTP)
+--- Forum: UFT / QTP Beginners (https://www.learnqtp.com/forums/Forum-UFT-QTP-Beginners)
+--- Thread: DateFormat help (/Thread-DateFormat-help)



DateFormat help - AutomationTester2011 - 05-31-2011

I need the script for displaying date format exactly as below..

eg: if date is may 31 2011 then it has to display as - "2011-05-31"

For example if the date is "01/02/2012" then it should display as "2012-02-01" and it should not display as "2012-2-1"

Please help




RE: DateFormat help - manabh - 05-31-2011

Hi
Use format(dtDate, "yyyy-MM-dd") function. Just check if the function is Format or FormatDate only.


RE: DateFormat help - AutomationTester2011 - 05-31-2011

Verified but its throwing error

currDate1 = FormatDateTime(dtDate, "yyyy-mm-dd")
PRINT currDate1

Type mismatch: '[string: "yyyy-mm-dd"]'

"currDate1 = FormatDateTime(dtDate, "yyyy-mm-dd")".



RE: DateFormat help - vIns - 05-31-2011

Hi... You can get Date in D, M, YYYY format using DatePart method. In order to display 5 as '05', You need to comeup with ur own function. As far as i know, there is no such method in VBScript.


RE: DateFormat help - sreekanth chilam - 05-31-2011

Hi,

You can try with the below ways:

Way1: Use FormatDateTime built-in function ( Refer the QTP help file for more info.)

Way2: Refer the below code

Code:
x="May 31 2011"
     Dpart=DatePart("d",x)    
     Mpart=DatePart("m",x)    
     Ypart=DatePart("yyyy",x)  
    
     If Dpart<=9 then
         Dpart="0"&Dpart
     End if  

     If Mpart<=9 then
         Mpart="0"&Mpart
     End if  

     RequiredDateVal=Ypart&"-"&Mpart&"-"&Dpart
     Msgbox RequiredDateVal




RE: DateFormat help - vIns - 05-31-2011

You can use this...

Code:
Function FormatYYYYMMDD(timeStamp)

    Dim dateMonth : dateMonth = DatePart("M", timeStamp)
    Dim dateDay : dateDay = DatePart("D", timeStamp)
    Dim dateYear : dateYear = DatePart("YYYY", timeStamp)
    Dim dateString

    dateString = dateYear

    If dateMonth < 10 Then
        dateString = dateString & "0" & dateMonth
    Else
        dateString = dateString & dateMonth
    End If

    If dateDay < 10 Then
        dateString = dateString & "0" & dateDay
    Else
        dateString = dateString & dateDay
    End If

    FormatYYYYMMDD = dateString
End Function



RE: DateFormat help - AutomationTester2011 - 05-31-2011

Vins and chalam

Sorry guys..just b4 i saw ur replies...i also got solution as below..thanks a lot

Code:
currDate = FormatDateTime(Date, 2)
year1 = right(currDate, 4)
month1 = mid(currDate, 4, 2)
day1 = left(currDate, 2)
newdateformat =year1& "-" &month1& "-" &day1
PRINT newdateformat