How to arbitrarily format a date variable


Peter Bradstreet
Hi David,

I have a scenario where I am taking a note record and putting it into local storage and also calling a rest service at the same time and inserting it into a MySQL database. I want to place an 'Updated' timestamp in both places so that I can compare the records to update the local record based on the remote record if the Updated value is greater in the database than the local version etc...

The problem that I am having is that the format that the Updated timestamp is being saved in local storage is not the same format that I need to submit to the rest service. Here is my insert code:

The local storage 'Updated' format is Sun Apr 21 2019 09:00:57 GMT-0500 (Central Daylight Time) but I need to submit it to the server as 2019-04-21 09:00:57:15

This seems like it should be fairly straight forward but I can't seem to figure out the correct syntax to do this. In a perfect world, I would like to store it locally and in MySql in the format: yyyy-MM-dd hh:mm:ss so that I can easily compare them.

Any help would be greatly appreciated!

Pete

decsoft

Hello Peter,

If the server is controlled by us, then we can submit the data in the format that we wanted, and, then format it again in the server if required (for example, to store in a database). You can use timestamps (maybe better for comparisson purposes?) or any date format. However, with the DateFormat action, we can certanly prepare a date string exactly in the format that we wanted, so, for example, the below code format a string just like you use in the server side:

Note that I use a different format string than what you shown: "yyyy-MM-dd hh:mm:ss": this is because the format string that I use follow the DateFormat action documentation, and this is what we must to do, of course.

In other words, if we try with your suggested format string "yyyy-MM-dd hh:mm:ss", we don't get the desired result. For your desired date format, the format string that we must use is "YYYY-MM-DD HH:mm:ss", just like you can see in the above piece of code.



Peter Bradstreet
Many thanks! Got it all working. Pete

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.