Tuesday, March 25, 2014

VBScript - Change Date Format

Use case:
For the following Calendar, set second date 2 days later that first date:
'parse initia date and change the format from 25.03.14 to 25/03/14
Dim fromDate, toDate, parsedDate, i, fromDay, fromMonth, fromYear
parsedDate = Split(Browser("Browser_Local").WebEdit("From").GetROProperty("value"),".")
fromDay = parsedDate(0)
fromMonth = parsedDate(1)
fromYear = parsedDate(2)
fromDate =    fromMonth & "/" & fromDay & "/" & fromYear
fromDate =    CDate(fromDate)
'set the second date 2 days after 03/25/14 => 03/27/14
toDate = DateAdd("d","2",fromDate)
'set the second date in the desired format => 27.03.14
Dim strDate
strDate = Right("0" & DatePart("d",toDate)2) & "." & Right("0" & DatePart("m",toDate)2) & "." & Right(DatePart("yyyy",toDate),2)
'enter the value for the second date Browser("Browser_Local").Page("Ihr Seminar, Kurs, Coaching").RunScript("$('#Hotel_overnight_to_1').val('"& strDate &"')") 
'following is optional. in my case these elements needed to have values also
Dim oPage
Set oPage=Browser("Browser_Local").Page("Ihr Seminar, Kurs, Coaching")
Dim bisDay, bisMonth, bisYear
Set bisDay=oPage.RunScript("document.getElementsByName('hotel[overnight_to][1][day]')(0);")
bisDay.Value=Right("0" & DatePart("d",toDate)2)
Set bisMonth=oPage.RunScript("document.getElementsByName('hotel[overnight_to][1][month]')(0);")
bisMonth.Value=Right("0" & DatePart("m",toDate)2)
Set bisYear=oPage.RunScript("document.getElementsByName('hotel[overnight_to][1][year]')(0);")
bisYear.Value=DatePart("yyyy",toDate)    

No comments:

Post a Comment