This workflow requests a date input from the user (in various formats) or a step of days starting from the current date. The input will then be converted into the designated target date using the pattern YYYY-MM-DD, which is the default format for Helmut4.
We begin with a Helmut4 Input Action, asking for a date or a date step:
The input is then passed into a JavaScript Action node, where an additional temporary variable is defined to set a maximum number of steps, ensuring that the user can't input a date like August 3rd, 2045.
The node will return either the calculated date or an error message.
The only limitation is with input formats: "dd MM yyyy" and "MM dd yyyy" would be interpreted incorrectly. Therefore, only the US format, "MM/dd/yyyy," is accepted.
However, the code can be adjusted to meet your specific format requirements.
// Check input for date pattern or integer
// Convert to date format yyyy-MM-dd
// Allows variants of yyyy MM dd / dd MM yyyy
// accepted input for US format: MM/dd/yyyy
// moovIT GmbH - Bernhard Dimmel
// Changelog: Aug 26th 2024
// Version 2.1
// Refactored complete logic to handle more input variants
// added integer input to define new period
// added error handling
// added max amount of extended date period via {stream.variable.maxPeriod}
// Function to check if a string is a valid date and return it in the format yyyy-MM-dd
function formatDate(input) {
// Array of possible date formats
var dateFormats = [
"yyyy-MM-dd", "yyyy_MM_dd", "yyyy.MM.dd", "yyyy MM dd",
"dd-MM-yyyy", "dd_MM_yyyy", "dd.MM.yyyy", "dd MM yyyy",
"MM/dd/yyyy"
];
// Function to parse and format a valid date
function parseAndFormatDate(input, dateFormats) {
for (var i = 0; i < dateFormats.length; i++) {
// Create a SimpleDateFormat object for the current format
var dateFormat = new java.text.SimpleDateFormat(dateFormats[i]);
// Set lenient to false to ensure strict date parsing
dateFormat.setLenient(false);
try {
// Parse the input string as a date
var parsedDate = dateFormat.parse(input);
// Format the parsed date back to the "yyyy-MM-dd" format
var formattedDate = new java.text.SimpleDateFormat("yyyy-MM-dd").format(parsedDate);
return formattedDate;
} catch (e) {
// Try the next format if parsing fails
}
}
return null;
}
// Function to check if the date exceeds the xx-day limit
function isDateExceedingLimit(date) {
var currentDate = new java.util.Date();
var calendar = java.util.Calendar.getInstance();
calendar.setTime(currentDate);
calendar.add(java.util.Calendar.DATE, {stream.variable.maxPeriod});
var limitDate = calendar.getTime();
return date.after(limitDate);
}
// First, try to parse the input as a date string
var formattedDate = parseAndFormatDate(input, dateFormats);
if (formattedDate) {
var parsedDate = new java.text.SimpleDateFormat("yyyy-MM-dd").parse(formattedDate);
if (isDateExceedingLimit(parsedDate)) {
return "Error: The extended period is too long - max period is {stream.variable.maxPeriod} days!";
}
return formattedDate;
}
// If input is not a valid date, check if it's an integer (number of days to add)
try {
var daysToAdd = parseInt(input);
if (!isNaN(daysToAdd)) {
var currentDate = new java.util.Date();
var calendar = java.util.Calendar.getInstance();
calendar.setTime(currentDate);
calendar.add(java.util.Calendar.DATE, daysToAdd);
var newDate = calendar.getTime();
if (isDateExceedingLimit(newDate)) {
return "Error: The extended period is too long - max period is {stream.variable.maxPeriod} days!";
}
return new java.text.SimpleDateFormat("yyyy-MM-dd").format(newDate);
}
} catch (e) {
// Handle parsing errors
}
return "Error: Not a valid date or integer input.";
}
// Define input and output
// Example: "2024-08-26" or "2"
var inputString = "{stream.variable.input-date}";
var result = formatDate(inputString);
// Return new date as yyyy-MM-dd
result;