I am currently building a workflow that will read a dueDate from a txt file. I created a local variable to get the dueDate. Then I also created a variable for the datetoday. I want this to be True if the duedate is 30 days before datetoday. But when i try to run the script below it always goes to false. Tried also to Watch.Log(dueDate) , but it wont give me the value from the local variable.
var dueDate = new Date(“${DueDate}”);
var dateToday = new Date(“${DateToday}”);
var timeDifference = dueDate.getTime() - dateToday.getTime();
var dayDifference = timeDifference / (1000 * 3600 * 24);
// Check if exactly 30 days before due date
if (dayDifference === 30) {
Script.ReturnValue = 1;
} else {
Script.ReturnValue = 0;
}
First, you can’t refer to process or global variables with ${Duedate}. The proper syntax is either:
Watch.ExpandString("%{DueDate}")
or
Watch.GetVariable("DueDate")
Second, process and global variables in Workflow are always of type String. So when using the new Date() syntax, you have to make sure that the string format you pass as a parameter will be interpreted correctly by the Date() object to automatically convert it to a date. But that conversion is highly dependent on your Windows Locale settings.
For instance, on my system::
// %{DueDate} contains "2025/06/01"
var dueDateTxt = Watch.ExpandString("%{DueDate}");
var dueDate = new Date(dueDateTxt);
var today = new Date();
Watch.log(datediff(today,dueDate),2); // Returns 32 when today is April 30
function datediff(first, second) {
return Math.round((second - first) / (1000 * 60 * 60 * 24));
}
But if the DueDate process variable contains 2025-06-01, then the result is undefined (because that format is not understood by the Date object).