Add days to date (variable)

Discuss RoboTask here
Post Reply
sarnusch
Posts: 54
Joined: Thu Apr 28, 2022 9:05 am

Add days to date (variable)

Post by sarnusch »

Hello,
I am using a python script to add days to a date that comes from a variable.
Calling the python script in a 'Dataset Loop' slows the execution of the task down.
Is there a way to handle the calculation without a script?

Code: Select all

from robotask import *

from datetime import datetime
from datetime import timedelta

rt = RoboTask()

new_date_format = "%Y%m%d"

dtm44 = rt.expand_text('{FIELDVALUE(5)}')
deliverytime = int(rt.expand_text('{FIELDVALUE(6)}'))

year = dtm44[6:10]
month = dtm44[3:5]
day = dtm44[:2]

given_date_string = year + "-" + month + "-" + day

#conversion from string to datetime object
given_date = datetime.strptime(given_date_string,"%Y-%m-%d")

#calculating new date
new_date = given_date + timedelta(days=deliverytime)

new_date = new_date.strftime(new_date_format)

rt.set_user_var('DTM17DATE', str(new_date))
Oleg
Site Admin
Posts: 3171
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: Add days to date (variable)

Post by Oleg »

Yes of course.
The example with VB evaluate action. By the way VB script is very fast in Windows™.

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|444033236
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task1533"
Hide=INTEGER|0
ID=INTEGER|84118130
LogOnAsUser=INTEGER|1
Name=STRING|"add days to date (VB evaluate)"
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
StepPause=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER
Action4=FOLDER

[Actions\Action1]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""dt1"" with value ""{Date}"""
Params=FOLDER

[Actions\Action1\Params]
_rt_variables_produced=STRING|"dt1"
expand=STRING|"1"
linecount=STRING|"1"
varname=STRING|"dt1"
varvalue=STRING|"{Date}"

[Actions\Action2]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""DeliveryDays"" with value ""5"""
Params=FOLDER

[Actions\Action2\Params]
_rt_variables_produced=STRING|"DeliveryDays"
expand=STRING|"1"
linecount=STRING|"1"
varname=STRING|"DeliveryDays"
varvalue=STRING|"5"

[Actions\Action3]
ActionID=STRING|"A_SCRIPT_VBEVALUATE"
Enabled=INTEGER|-1
Name=STRING|"VB Evaluate"
Params=FOLDER

[Actions\Action3\Params]
_rt_variables_produced=STRING|"dt2"
expression=STRING|"dateadd(""d"",{DeliveryDays},cdate(""{dt1}""))"
linecount=STRING|"0"
loadfromfile=STRING|"1"
variable=STRING|"dt2"

[Actions\Action4]
ActionID=STRING|"A_DIALOG_MESSAGE"
Enabled=INTEGER|-1
Name=STRING|"Show results"
Params=FOLDER

[Actions\Action4\Params]
icon=STRING|"1"
msg0=STRING|"begin date = {dt1}"
msg1=STRING|"delivery days = {DeliveryDays}"
msg2=STRING|"new date = {dt2}"
msgcount=STRING|"3"
playsound=STRING|"0"
showmessage=STRING|"1"
Next example uses only internal actions. It uses Value format action and Add system variable

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|-1
CatID=INTEGER|444033236
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task1534"
Hide=INTEGER|0
ID=INTEGER|-1919157832
LogOnAsUser=INTEGER|1
Name=STRING|"add days to date (internal actions)"
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
StepPause=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER
Action4=FOLDER
Action5=FOLDER

[Actions\Action1]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""dt1"" with value ""{Date}"""
Params=FOLDER

[Actions\Action1\Params]
_rt_variables_produced=STRING|"dt1"
expand=STRING|"1"
linecount=STRING|"1"
varname=STRING|"dt1"
varvalue=STRING|"{Date}"

[Actions\Action2]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""DeliveryDays"" with value ""5"""
Params=FOLDER

[Actions\Action2\Params]
_rt_variables_produced=STRING|"DeliveryDays"
expand=STRING|"1"
linecount=STRING|"1"
varname=STRING|"DeliveryDays"
varvalue=STRING|"5"

[Actions\Action3]
ActionID=STRING|"A_VALUEFORMAT"
Enabled=INTEGER|-1
Name=STRING|"Value format (Systemtime->DateAsNumber)"
Params=FOLDER

[Actions\Action3\Params]
_rt_variables_produced=STRING|"FormatResult"
format=STRING|"Systemtime"
fromvalue=STRING|"{dt1}"
group=STRING|"Time"
resultvar=STRING|"FormatResult"
reverse=STRING|"1"
value=STRING|"DateAsNumber"

[Actions\Action4]
ActionID=STRING|"A_VALUEFORMAT"
Enabled=INTEGER|-1
Name=STRING|"Value format (DateAsNumber->Systemtime)"
Params=FOLDER

[Actions\Action4\Params]
_rt_variables_produced=STRING|"dt2"
format=STRING|"Systemtime"
fromvalue=STRING|"{Add({FormatResult},{DeliveryDays})}"
group=STRING|"Time"
resultvar=STRING|"dt2"
reverse=STRING|"0"
value=STRING|"DateAsNumber"

[Actions\Action5]
ActionID=STRING|"A_DIALOG_MESSAGE"
Enabled=INTEGER|-1
Name=STRING|"Show results"
Params=FOLDER

[Actions\Action5\Params]
icon=STRING|"1"
msg0=STRING|"begin date = {dt1}"
msg1=STRING|"delivery days = {DeliveryDays}"
msg2=STRING|"new date = {dt2}"
msgcount=STRING|"3"
playsound=STRING|"0"
showmessage=STRING|"1"
Try these examples
Oleg Yershov
sarnusch
Posts: 54
Joined: Thu Apr 28, 2022 9:05 am

Re: Add days to date (variable)

Post by sarnusch »

Hi,
the second example works fine for me. Thank you very much!
sarnusch
Posts: 54
Joined: Thu Apr 28, 2022 9:05 am

Re: Add days to date (variable)

Post by sarnusch »

Hello,
I want to ask if it is possible to modify the task (with internal actions or VB script) so that only working days are added?
Oleg
Site Admin
Posts: 3171
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: Add days to date (variable)

Post by Oleg »

Hm-m...
Working days are not just from Monday to Friday
There are also public holidays. And the holidays are different in different countries.
Oleg Yershov
sarnusch
Posts: 54
Joined: Thu Apr 28, 2022 9:05 am

Re: Add days to date (variable)

Post by sarnusch »

For my example, it is sufficient that the working days are Monday to Friday.
I tried to implement the following VB script in RoboTask but I could not get it to work with the "VB Script" and "VB Evaluate":

Code: Select all

Dim startdate As Date
Dim enddate As Date
Dim daysToAdd As integer
startDate = "01-10-2025"
daysToAdd = 10
enddate = startDate
While daysToAdd > 0
	enddate = DateAdd("d", 1, enddate)
	If enddate.DayOfWeek() > 0 and enddate.DayOfWeek() < 6 Then
		daysToAdd = daysToAdd - 1
	End If
End While
System.Console.WriteLine(enddate)
How can I pass my date in a variable to the script and get a result from the script?
Oleg
Site Admin
Posts: 3171
Joined: Thu Jan 01, 1970 1:00 am
Contact:

Re: Add days to date (variable)

Post by Oleg »

An adapted script is given below

Code: Select all

function AddWorkingDays(StartDate, WorkingDays)
  StartDate = cdate(StartDate)
  enddate = startDate
  while WorkingDays > 0
    enddate = DateAdd("d", 1, enddate)
    wd = Weekday(enddate)
    '1 = sunday
    '7 = saturday
    If (wd > 1) and (wd < 7) Then
		  WorkingDays = WorkingDays - 1
    end if
  wend
  AddWorkingDays = enddate
end Function
Also look at the my example below

Code: Select all

;*****************************
;* RoboTask Task file
;* Do not edit in text editor!
;*****************************
 
[Root]
ActionAfterRun=INTEGER|0
Actions=FOLDER
Automat=INTEGER|0
CatID=INTEGER|45174721
Comment=STRINGLIST
ContinueOnError=INTEGER|0
DisableOnError=INTEGER|0
DoNotStopWhenShutdown=INTEGER|0
ExternalName=STRING|"Task1468"
Hide=INTEGER|0
ID=INTEGER|-523817162
LogOnAsUser=INTEGER|1
Name=STRING|"Add working days only"
OnErrorTaskID=INTEGER|0
Priority=INTEGER|3
RestrictRESTAPIAccess=INTEGER|0
RunOnClose=INTEGER|0
RunOnStartup=INTEGER|0
StepPause=INTEGER|0
ToLog=INTEGER|3
UnicodeFormat=INTEGER|1
WriteGeneralLog=INTEGER|0

[Actions]
Action1=FOLDER
Action2=FOLDER
Action3=FOLDER
Action4=FOLDER

[Actions\Action1]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""startDate"" with value ""{Date}"""
Params=FOLDER

[Actions\Action1\Params]
_rt_variables_produced=STRING|"startDate"
expand=STRING|"1"
linecount=STRING|"1"
varname=STRING|"startDate"
varvalue=STRING|"{Date}"

[Actions\Action2]
ActionID=STRING|"A_VARIABLES_SET"
Enabled=INTEGER|-1
Name=STRING|"Set variable ""WorkingDays"" with value ""11"""
Params=FOLDER

[Actions\Action2\Params]
_rt_variables_produced=STRING|"WorkingDays"
expand=STRING|"0"
linecount=STRING|"1"
varname=STRING|"WorkingDays"
varvalue=STRING|"11"

[Actions\Action3]
ActionID=STRING|"A_SCRIPT_VBEVALUATE"
Enabled=INTEGER|-1
Name=STRING|"VB Evaluate"
Params=FOLDER

[Actions\Action3\Params]
_rt_variables_produced=STRING|"NewDate"
expression=STRING|"AddWorkingDays(""{StartDate}"",{WorkingDays})"
line00000000=STRING|"function AddWorkingDays(StartDate, WorkingDays)"
line00000001=STRING|"  StartDate = cdate(StartDate)"
line00000002=STRING|"  enddate = startDate"
line00000003=STRING|"  while WorkingDays > 0"
line00000004=STRING|"    enddate = DateAdd(""d"", 1, enddate)"
line00000005=STRING|"    wd = Weekday(enddate)"
line00000006=STRING|"    '1 = sunday"
line00000007=STRING|"    '7 = saturday"
line00000008=STRING|"    If (wd > 1) and (wd < 7) Then"
line00000009=STRING|"		  WorkingDays = WorkingDays - 1"
line0000000A=STRING|"    end if"
line0000000B=STRING|"  wend"
line0000000C=STRING|"  AddWorkingDays = enddate"
line0000000D=STRING|"end Function"
line0000000F=STRING|"'d = AddWorkingDays(""10.01.2025"", 2)"
line00000010=STRING|"'LogMessage(d)"
linecount=STRING|"17"
loadfromfile=STRING|"1"
variable=STRING|"NewDate"

[Actions\Action4]
ActionID=STRING|"A_ROBOTASK_LOG"
Enabled=INTEGER|-1
Name=STRING|"Log Message"
Params=FOLDER

[Actions\Action4\Params]
message=STRING|"start = {startDate}{eol}New date = {NewDate}{eol}Woking days ={WorkingDays}"
type=STRING|"3"

Oleg Yershov
sarnusch
Posts: 54
Joined: Thu Apr 28, 2022 9:05 am

Re: Add days to date (variable)

Post by sarnusch »

Good morning,
thank you very much!
I will implement it into my task.
Post Reply