sebastienros/fluid

Date Format not persisting milliseconds

Closed this issue · 3 comments

Hi,

The milliseconds are dropped when converting a date using fluid. Here is a working sample in liquidjs:
https://liquidjs.com/playground.html#ewogIlRlc3REYXRlVGltZSI6ICJ7e1Rlc3REYXRlVGltZXwgZGF0ZTogIiVZLSVNLSVkVCVIOiVNOiVTLiVMIn19IgogIlRlc3REYXRlVGltZVdpdGhDb25zdGFudCI6ICJ7eyIyMDIyLTA5LTAxVDA3OjA5OjM1Ljg3OSJ8IGRhdGU6ICIlWS0lTS0lZFQlSDolTTolUy4lTCJ9fSIKfQ==,eyAiVGVzdERhdGVUaW1lIjogIjIwMjItMDktMDFUMDc6MDk6MzUuODc5IiB9

When I run the same test in Fluid the milliseconds are removed for the variable. They are persisted for the constant value.

Input:
{ "TestDateTime": "2022-09-01T07:09:35.879" }

Transform:
{ "TestDateTime": "{{TestDateTime| date: "%Y-%M-%dT%H:%M:%S.%L"}}" "TestDateTimeWithConstant": "{{"2022-09-01T07:09:35.879"| date: "%Y-%M-%dT%H:%M:%S.%L"}}" }

Output:
{ "TestDateTime": "2022-09-01T07:09:35.000" "TestDateTimeWithConstant": "2022-09-01T07:09:35.879" }

@reidono1 are you planning to submit a PR?

There is already a test for that format: https://github.com/sebastienros/fluid/blob/main/Fluid.Tests/MiscFiltersTests.cs#L225

Can you ensure the issue is not coming from the way you are parsing the date time? You are also proving that the ms is correctly outputted with the static way.

FYI: there is a mistake in your code, months is %m and not %M. You are not seeing the issue because in your example they are both 9.

Thanks for pointing out the discrepancy with the month format @sebastienros!

The issue was on my side. I was deserializing the input as Json, using Newtonsoft. I needed to add the following SerializationSettings to output the date with milliseconds:

var deserializedInput = JsonConvert.DeserializeObject( input, new JsonSerializerSettings { DateParseHandling = DateParseHandling.None });