Calculating hours between private data timestamp and current time (no scripting module)

Post Reply
jcarden
Newbie
Posts: 19
Joined: Sun Feb 26, 2023 5:09 am

Calculating hours between private data timestamp and current time (no scripting module)

Post by jcarden »

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!
jan_suhr
Advanced member
Posts: 694
Joined: Fri Nov 04, 2011 1:12 pm
Location: Nyköping, Sweden

Re: Calculating hours between private data timestamp and current time (no scripting module)

Post by jan_suhr »

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.
Jan Suhr
Color Consult AB
Sweden
=============
Check out my apps
sander
Advanced member
Posts: 310
Joined: Wed Oct 01, 2014 8:58 am
Location: Den Bosch

Re: Calculating hours between private data timestamp and current time (no scripting module)

Post by sander »

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.
theDuration.png
theDuration.png (76.77 KiB) Viewed 100 times

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";
    }
}
https://www0.enfocus.com/en/appstore/pr ... ivate-data
Post Reply