JSON date and time


Ryan Sytsma

I get JSON date and time values from an API call.

I need to format the date and times for display. Would this code do what I am trying to do?



DecSoft

Hello Ryan,

A couple of things. To format a date we must create a date variable first, so the way is the below one:

The other thing is related with the "ObjectGetProp" action. It's no problem to use it, but, for your knowledge, it's also possible to directly access the object properties from the server's response, so, it's possible to write something like the below one:



Ryan Sytsma

I followed the pattern you pointed out here:

All I get in the Alert is "Invalid Date". If I declare NewDate with a value and format, why do I need to call DateFormat?

Can the date formats be MM/DD/YYYY?



DecSoft

Hello Ryan,

With "NewDate" you create a new date variable, and, since you do it using a value, you specify the format of that value, in order to properly create the date. Once you have a date variable, and, if you want to show it, then you can use the "DateFormat" action.

I think the problem here can be the time format, that is, probably we can't create a date variable only with time information / format. So, if we can't create the date variable correctly, we can't format that non valid date variable.



Ryan Sytsma

Are there any suggestions of how to handle this issue? Maybe JS would handle it better? Could I format the time values as a date and time like MM/DD/YYYY HH:MM A and then use DateGet to build a string representing the time?



DecSoft

Hello Ryan,

I think we are missing something here, because, in fact there is no issues, that is, take a look at the below code:

This time we take in consideration both date and time, so, what "[FormattedDate]" stores is "2019-09-15 21:22:23". In other words, it's perfectly possible to create a date variable which include the date and the time.

Maybe I confuse you because I mention above that we can't create a date variable only with the time... and this is truth, that is, only with the time we can't create a date variable, but, we can create date variables (as you can see below) that stores both date and times values.

Once we have a date variable, we can use several available actions to extract the days, month, etc. Or just to show the date variable in certain format, like the code above does.



Ryan Sytsma
David, thanks for your help. I went ahead and formatted the date and time on the C# server side code in the API. It returns in proper format in the GET response now.

DecSoft

Hello Ryan,

It's another way, of course, but, please, don't use the server... if you want to use the client... as you can see above, it's possible to create a date variable from an arbitrary value, and, then it's also possible to show that date variable in certain specific format. It's possible, so, if you find any trouble... just post it here and we will see...


Everybody can read the DecSoft support forum for learning purposes, however only DecSoft customers can post new threads. Purchase one or more licenses of some DecSoft products in order to give this and other benefits.

This website uses some useful cookies to store your preferences.

I agree. Hide this note. Give me more information.