Getting the current Date or DateTime

The Computational Helpers page documents the Now() general function to get the current UTC time. I am trying to use this in order to do a workflow computation to calculate the Target Time - Current Time to work out the number of days until an inspection is due. I have tried multiple different ways of accessing the current datetime but each one gives me an error:

“return (targetTime.Value - Now()).TotalDays;”

gives:

“Non-invocable member ‘HostBase.Now’ cannot be used like a method.”

I’ve tried moving it into a variable and using DateTime.Now to return it but I’ve been getting errors along the lines of:

‘HostBase.DateTime(int, int, int, int, int, int, int)’ is a method, which is not valid in the given context

It does feel at times that the documentation only tells half the story. If there are examples you can inspect/copy you can just about get by but if there aren’t any specific examples, it is shooting in the dark at times.

Hi Chris,

It seems you don’t need the parentheses after that Now

So in your first example it should be something like
"script": "return (int)((targetTime.Value - Now).TotalDays);"

(TotalDays returns a double, so it must be cast to int if you need an integer)

Also bear in mind that the attribute type needs to match the computation, so if what you needed was to display the days as a string, you’d need:
"script": "return ((int)((targetTime.Value - Now).TotalDays)).ToString();"

Hope this helps!

Thanks Niko however that doesn’t seem to have worked.

I tried your first solution "script": "return (int)((targetTime.Value - Now).TotalDays);" but that returned me the following:

AlloyException: WorkflowScriptExecutionFailed: E1623243445: InvalidOperationException: Nullable object must have a value. Workflow script execution had errors.

So I tried it as Now.Value but that gave me:

“Operator ‘-’ cannot be applied to operands of type ‘DateTime’ and ‘method group’”

So I tried return (int)((targetTime.Value - Local(Now)).TotalDays); and this gives me

“error”: “‘TimeSpan?’ does not contain a definition for ‘TotalDays’ and no accessible extension method ‘TotalDays’ accepting a first argument of type ‘TimeSpan?’ could be found (are you missing a using directive or an assembly reference?)”,

Which is weird because I’ve used .TotalDays in a different workflow calculation and that one works.

I managed to fix it in the end. Try, try and try again.

A big fix that helped was to amend the upstream AQS query that was feeding this computation to ensure that it only spat out inspections where the Target Time attribute was set. That’s where I think a lot of it was falling over.

Then I moved everything into the variables as scripts to convert them to dates. So the parameter uses the script return (int)((targetDate.Value - todayDate.Value).TotalDays);,

With the variables coming in as:

    {
        "name": "targetTime",
        "value": {
            "inputParent": 0,
            "itemValue": {
                "attributeCode": "attributes_tasksTargetTime",
                "discriminator": "WorkflowSyntaxArgumentItemValueAttributeWebModel"
            },
            "discriminator": "WorkflowSyntaxNodeInputWebModel"
        }
    },
    {
        "name":"targetDate",
        "value":{
            "discriminator":"WorkflowSyntaxNodeScriptWebModel",
            "script": "var targetDate = Date(targetTime); return targetDate;"
        }
    },
    {
        "name":"todayDate",
        "value":{
            "discriminator":"WorkflowSyntaxNodeScriptWebModel",
            "script":"var todayDate = Date(Now); return todayDate;"
        }
    }]

This does however leave me a problem relating to DateTimes and daylight savings time but I’ll post a separate topic on that.