NJsonSchema icon indicating copy to clipboard operation
NJsonSchema copied to clipboard

Handle DateOnly in typescript client as local date (not UTC)

Open Shaddix opened this issue 3 years ago • 2 comments

DateOnly is serialized to JSON as 'yyyy-MM-dd' (e.g. '2022-01-16'). When it's deserialized (in JS), NJsonSchema uses new Date('2022-01-16'). Considering I'm in New York (UTC -5), new Date('2022-01-16').toLocaleString() gives me 1/15/2022, 7:00:00 PM, and new Date('2022-01-16').toLocaleDateString() gives 1/15/2022. Moreover, if I now send this received DateOnly back to my API, it'd be sent as 2022-01-15, because formatDate (used in NJsonSchema for DateOnly serialization) uses local date.

My personal expectation was to get it deserialized as 1/16/2022, 12:00:00 AM (regardless of my time zone).

I understand, that JS by default interprets 'yyyy-MM-dd' as UTC date, but imo it'd be correct to adjust DateOnly fields after deserialization (or at least to make it configurable/provide some callbacks that I could set to adjust deserialization for DateOnly fields)

Shaddix avatar Jan 15 '22 17:01 Shaddix

btw, Luxon, Moment, DayJs interpret '2022-01-16' in local timezone, so only native js dates are affected (i.e. TypeScriptDateTimeType.Date)

Shaddix avatar Jan 15 '22 18:01 Shaddix

I could PR that if you are ok with the idea and could help me with name for setting (in TypeScriptGeneratorSettings), that will enable that behavior :)

Shaddix avatar Jan 15 '22 18:01 Shaddix