Hey everyone —
I’m trying to calculate the number of hours elapsed between a private data timestamp and the current system time inside Switch.
Here’s what I’m working with:
• Private data key: mil_ArtToProduction
• Value format: 2025-10-24T16:00:00Z
• Goal: Create a new private data key (for example, HoursSinceArtToProduction) that represents how many hours have passed between that timestamp and now.
The challenge is that I don’t have the Scripting Module, so I can’t rely on JavaScript or custom scripting to handle the calculation directly. I’ve tried a few different approaches using built-in Switch tools (Private Data Magician, Date Calculator, etc.), but it seems like Switch doesn’t interpret the ISO timestamp as a date/time — it just sees it as plain text.
Has anyone found a reliable way to perform this kind of calculation without the scripting module? Ideally I’d like to calculate the elapsed time in hours and store it as a new private data key for use later in the flow.
Any advice or examples would be greatly appreciated!
Calculating hours between private data timestamp and current time (no scripting module)
Re: Calculating hours between private data timestamp and current time (no scripting module)
One of the most popular apps can fix that for you
https://www0.enfocus.com/en/appstore/pr ... calculator
It can give you Private data for the numeric value of a datetime (down to milliseconds) and from that you can calculate the difference between two.
https://www0.enfocus.com/en/appstore/pr ... calculator
It can give you Private data for the numeric value of a datetime (down to milliseconds) and from that you can calculate the difference between two.
Re: Calculating hours between private data timestamp and current time (no scripting module)
You can use 'Script Private Data' to set a Private Data value with a Script expression. Use that to do some calculation and return the duration.
I have tested the start and end values with this date format: 2025-01-01T13:00:00.000Z.
https://www0.enfocus.com/en/appstore/pr ... ivate-data
I have tested the start and end values with this date format: 2025-01-01T13:00:00.000Z.
Code: Select all
async function calculateScriptExpression(s: Switch, flowElement: FlowElement, job: Job): Promise<string> {
try {
// const start = await job.getVariableAsString('[Metadata.Text:Dataset="jdf.return",Model="JDF",Path="/jdf:JDF/jdf:AuditPool[1]/jdf:PhaseTime[1]/jdf:ModulePhase[@ModuleType=\'Printing\']/@Start"]');
const start = await job.getVariableAsString('[Metadata.Text:Dataset="jdf.return",Model="JDF",Path="//jdf:ModulePhase[@ModuleType=\'Printing\']/jdf:GeneralID[@IDUsage=\'hpPrintingStartTime\']/@IDValue"]');
const end = await job.getVariableAsString('[Metadata.Text:Dataset="jdf.return",Model="JDF",Path="//jdf:ModulePhase[@ModuleType=\'Printing\']/@End"]');
// Parse as date
const startDate = new Date(start);
const endDate = new Date(end);
// Check if valid
if (isNaN(startDate.getTime()) || isNaN(endDate.getTime())) {
throw new Error('Invalid start or end date format');
}
// Calculate difference in milliseconds
const diffMs = endDate.getTime() - startDate.getTime();
// Convert milliseconds to minutes
const diffMinutes = Math.max(1, Math.ceil(diffMs / (1000 * 60)));
// Return the difference in minutes as string
return diffMinutes.toString();
} catch (error: any) {
await job.log(LogLevel.Error, `Error calculating minutes between start and end: ${error.message}`);
return "0";
}
}