I need a date in "dd-mm-yyyy" format in json. But it seems date is always given in "yyyy-mm-dd" fomat in json. What should i do to get date in "dd-mm-yyyy" format in write-json/rest api response? Please dont suggest use of datatype "character".
There is one and only one date format in the JSON format: yyyy-mm-dd.
Both the producer and the consumer must adapt themselves to this format.
Can you please tell why you would need something else?
[quote user="Jean-Christophe Cardot"]There is one and only one date format in the JSON format: yyyy-mm-dd.[/quote]
This is not true. The official JSON standards (see https://www.json.org/) do not specify any standard format for date/datetime related data.
Most people go with yyyy-mm-dd because that follows the ISO 8601 standards for date formatting because filling the gap in the JSON standards with another, well documented and pre-existing standard just makes a lot of sense. But that's decided by platform and implementation of the JSON provider/consumer, and not something that's guaranteed at all.
In JSON it is formatted like that but if you map the value to a date field in OpenEdge it will be converted for you to the correct date I believe.
Add your own datatypes. Fx
CLASS Json.MyJson INHERITS JsonObject: METHOD PUBLIC LOGICAL AddMyDate( INPUT propertyName AS CHARACTER, INPUT dValue AS DATE): DEFINE VARIABLE cDate AS CHARACTER NO-UNDO. cDate = STRING(dValue, "99/99/9999"). // Your date formatting to string. RETURN THIS-OBJECT:Add(propertyName, cDate). END METHOD. METHOD PUBLIC DATE GetMyDate( INPUT propertyName AS CHARACTER): DEFINE VARIABLE cDate AS CHARACTER NO-UNDO. cDate = THIS-OBJECT:GetCharacter(propertyName). RETURN DATE(cDate). // YOur conversion from string to date END METHOD. END CLASS.
Is there an equivalent to C# TryParse to avoid using too many CATCH phrase when GetSomeType() fails?