Server side date functions

Hi everyone,
Can someone please explain to me how to do date algebra on the server side?

There are 3 functions on the server side: Format Date, Date Add, and Date Diff, as shown in the pic below:

All my attempts to use any of these 3 functions have failed rather miserably. I don’t know if this is me or it is a bug. I hope it’s me and that someone sets me on the right path, but it does look like a bug.

How to reproduce:

Let’s say that I have 2 dates, that I already passed to the server action (and I know it received it well, because I stored them in a table and are correct):
$_POST.ItemStarts
$_POST.ItemEnds

Date Add

Let’s say that I want to add 2 days to one of them. I use the Date Add function, which creates the following binding (I hope I’m using the correct term here) :

{{$_POST.ItemStarts.dateAdd(“days”, 2)}}

I tried three things:

a) Assign this binding to a $_SESSION variable, like in
Set Session Result1 = {{$_POST.ItemStarts.dateAdd(“days”, 2)}}
(I tried defining this session variable both as text and as date and the results were the same)

b) Assign this binding to a variable (don’t know if they have any special name), like in Set Value Result2 = {{$_POST.ItemStarts.dateAdd(“days”, 2)}}
(I don’t know how to specify the type of a value variable)

c) Assign this binding to a table field, as shown below:


(I tried doing this both in a text field and in a date field and the results were the same)

None of these three operations works and the server action dies. If I don’t have the “set” commands and just try to insert the record, the action doesn’t happen.

Date Diff
Let’s say that I want to know the number of days between my two dates:

Using the “formatter”, I get the following string:

{{$_POST.ItemEnds.dateDiff(“days”, $_POST.ItemStarts)}}

I tried all three of the above approaches with the same results.

Am I doing something wrong? I’m totally stuck.
This is a batch process, so I can’t pass all the values from the client side and I need those calculations done.

Many thanks in advance!!

Alex

Hello, what values do your POST variables return exactly?

I pass dates to the server action, in the form of YYYY-MM-DD. Example: 2020-01-28 for today.

What do you mean you paste dates in the server action? Are these text inputs where you paste these dates, and you are not using the date picker component?

Hi Teodor,
My question is all about the server side. The form that was used to post values to the server action does use date pickers but that’s not the issue.

I need the server action to be able to do “date algebra” (add dates to a date, find the number of days between two dates) using the dates that it received as POST variables.

Does that make sense?

Alex

It is important what data do you send and how do you send it to your server action. That’s why I am asking what exactly and how exactly do you send with your POST variables?

I collect dates using a server action form and pass them to the server action by clicking submit. As said before, one of the steps in the sever action saves those dates as passed in a DB table – and that works fine.

There are 3 key dates passed by the form to the server.
All dates are sent as YYYY-MM-DD.

ItemStart is usually entered by the user through a date picker widget. It’s formatted by the date picker as shown below.

ItemEnd is usually calculated in the form, and presented as the value of a date picker. An example of this could be:
(ltd_120Months.value < sc_PMEMB_GetOne.data.query1.LTD65) ? ltd_120Months.value : sc_PMEMB_GetOne.data.query1.LTD65
where the date is arrived at as the minimum between two dates.

TransactionDate is today’s date taken from the today.datetime component (in days) and formatted as YYYY-MM-DD. The actual value is today.datetime.formatDate(“yyyy-MM-dd”)

Hope this helps. Let me know otherwise.

PS: I also created a smaller form with just a couple of date pickers and a dummy server action to reproduce the issues. In this simplified form, all the dates are inputted using a date picker. I can reproduce them.

Then everything must be working perfectly fine.

I just set up a quick test to check this and the results are as expected.

On the front end i have a server connect form with two date pickers:

In the server action they are available under Globals > POST.
I added two set value steps to just output the results, the first one uses dateAdd formatter applied on the first date picker (POST var). The second one uses dateDiff formatter, which checks the difference in days between the second and first date pickers (POST vars):

Screenshot_19

The result from the server action, when you run the page in the browser is just what you can expect:

I must be doing something wrong then. Thank you!
I’ll check it tomorrow and let you know.

You can try outputting the results the same way as me - using set values, to see if the result is fine.

Just tried it and got the same problem as before.
I even set the Set Values as output, to see them directly in the page.
It’s blank.
I copied your commands verbatim.

What if you just output the POST vars in set value steps, without any formatting?

that works fine. I can see them.

Ok please send me a link to your page, where i can check the output of set variable steps of your server action.

Here it is:
https://mars7.serverzilla.com/webCases/test02.php

And here is the server action

Hope this helps,

Alex

But there is some issue with your server action, it returns the following status (error 500)

Please remove this response step from the catch! I need to see the exact fail reason.

I know. That’s why I’m asking for help.
I put the return 500.

Please remove this response step from the catch! I need to see the exact fail reason.

Sorry, I put that to see if at least it run. It’s removed now.