VBS Date manipulation/formatting (calculating "yesterday")

Information related to the use of iMacros for form filling and data upload.
Forum rules
iMacros EOL - Attention!

The renewal maintenance has officially ended for Progress iMacros effective November 20, 2023 and all versions of iMacros are now considered EOL (End-of-Life). The iMacros products will no longer be supported by Progress (aside from customer license issues), and these forums will also no longer be moderated from the Progress side.

Thank you again for your business and support.

Sincerely,
The Progress Team
Post Reply
Hannes, Tech Support

VBS Date manipulation/formatting (calculating "yesterday")

Post by Hannes, Tech Support » Mon Jul 28, 2008 9:25 am

Here's a script to generate a CSV file that contains yesterday's date in several formats you can then use in your macro (via the DATASOURCE commands, cf. http://wiki.imacros.net/CSV_input). The result looks like this:

Code: Select all

7/27/2008,7,07,27,27,2008,July
Script:

Code: Select all

Option Explicit

Dim INPUT_FILE_PATH

' GLOBAL VARIABLES (change to adjust to your system)
INPUT_FILE_PATH = "c:\yesterday.date.csv"

' File access constants
Const FOR_READING = 1
Const FOR_WRITING = 2
Const FOR_APPENDING = 8 

'open file for writing
Dim objFileSystem,inputFile
Set objFileSystem = CreateObject("Scripting.fileSystemObject")
Set inputFile = objFileSystem.OpenTextFile(INPUT_FILE_PATH, FOR_WRITING, true)

'calculate yesterday date by adding "-1" days to today's date
Dim todayDate
todayDate=Date()
msgbox "today: "+Cstr(todayDate)

Dim yesterdayDate
yesterdayDate = DateAdd("d", -1, todayDate)
msgbox "yesterday: "+Cstr(yesterdayDate)

' format yesterday's date, so the result file contains the full date, and month, day, year in several formats
Dim yesterdayDay, yesterdayDayDoubleDigit, yesterdayMonth, yesterdayMonthDoubleDigit, yesterdayYear, yesterdayMonthName
yesterdayDay = Day(yesterdayDate)
if yesterdayDay < 10 then
	yesterdayDayDoubleDigit = "0"+Cstr(yesterdayDay)
else
	yesterdayDayDoubleDigit = yesterdayDay
end if

yesterdayMonth = Month(yesterdayDate)
if yesterdayMonth < 10 then
	yesterdayMonthDoubleDigit = "0" + Cstr(yesterdayMonth)
else
	yesterdayMonthDoubleDigit = yesterdayMonth
end if

yesterdayYear = Year(yesterdayDate)
yesterdayMonthName = MonthName(yesterdayMonth)


'display results
msgbox "in separate columns - full date: "+Cstr(yesterdayDate)+", month: "+Cstr(yesterdayMonth) + ", month (double digit): "+Cstr(yesterdayMonthDoubleDigit)+", day: "+Cstr(yesterdayDay) + ", day (double digit): "+Cstr(yesterdayDayDoubleDigit)+", year: "+Cstr(yesterdayYear)+", monthname: " + Cstr(yesterdayMonthName)

'write date strings to file
inputFile.write(Cstr(yesterdayDate) + "," + Cstr(yesterdayMonth) + "," + Cstr(yesterdayMonthDoubleDigit) + "," + Cstr(yesterdayDay) + "," + Cstr(yesterdayDayDoubleDigit)+ "," + Cstr(yesterdayYear) + "," +Cstr(yesterdayMonthName)) 

'close fileaccess
inputFile.close()
objFileSystem = NULL 

msgbox "End."

' exit script
Wscript.Quit()

[Edit: Added link to DATASOURCE commands (for using CSV input in macro)]
Hannes, Tech Support

Use calculated date in macro

Post by Hannes, Tech Support » Thu Aug 07, 2008 8:02 am

In the solution above, the calculated date was written to a CSV file, so some macro can use it using the DATASOURCE commands.

In the sample below, the VBS code uses the calculated date directly in creating the macro (in a string):

Code: Select all

Option Explicit

'calculate date. here: adding 4 months to today's date
Dim todayDate
todayDate=Date()
msgbox "today: "+Cstr(todayDate)

Dim newDate
newDate = DateAdd("m", 4, todayDate)
msgbox "calculated date: "+Cstr(newDate)

'initiate iMacros instance, create macro using the calculated date, then run macro
' (note that you could also pass the newDate value via iimSet() to some macro which is then called directly by iimPlay() - instead of putting the macro in a string, as it is done, here)
Dim iim1, iret
set iim1 = CreateObject ("imacros")
iret = iim1.iimInit()

Dim macro
macro = "CODE:"
macro = macro + "VERSION BUILD=6200707     " + vbNewLine
macro = macro + "TAB T=1     " + vbNewLine
macro = macro + "TAB CLOSEALLOTHERS     " + vbNewLine
macro = macro + "URL GOTO=http://www.iopus.com/imacros/demo/v6/f1/form.asp     " + vbNewLine
' in this line, we use the calculated date
macro = macro + "TAG POS=1 TYPE=TEXTAREA FORM=NAME:TestForm ATTR=NAME:Remarks CONTENT="+Cstr(newDate)

iret = iim1.iimPlay(macro)

'end
msgbox "End. We'll close the iMacros browser and exit the VBS script."

'close iMacros instance
iret = iim1.iimExit()

' exit script
Wscript.Quit()
Post Reply