VBS Date manipulation/formatting (calculating "yesterday")

Information related to the use of iMacros for form filling and data upload.
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


Code: Select all

Option Explicit


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

' File access constants

'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
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)
	yesterdayDayDoubleDigit = yesterdayDay
end if

yesterdayMonth = Month(yesterdayDate)
if yesterdayMonth < 10 then
	yesterdayMonthDoubleDigit = "0" + Cstr(yesterdayMonth)
	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
objFileSystem = NULL 

msgbox "End."

' exit script

[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
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)

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

'close iMacros instance
iret = iim1.iimExit()

' exit script
Post Reply