How to get date in handlebar template 'yyyy-MM-dd' format #652

Closed
opened 2025-12-29 08:31:51 +01:00 by adam · 2 comments
Owner

Originally created by @jusski on GitHub (Dec 20, 2024).

Originally assigned to: @StefH on GitHub.

If i set mapping to return date in "yyyy-MM-dd" format

"Response": {
    "StatusCode": 200,
    "BodyAsJson": {
      "date": "{{String.Format (DateTime.UtcNow) \"yyyy-MM-dd\"}}"
    },
    "Headers": {
      "Content-Type": "application/json; charset=utf-8"
    },
    "UseTransformer": true,
    "TransformerType": "Handlebar"
  }

im getting back wrong date format:
{ "date": "2024-12-20T00:00:00" }
Tried Scriban transformer too, with same results

Originally created by @jusski on GitHub (Dec 20, 2024). Originally assigned to: @StefH on GitHub. If i set mapping to return date in "yyyy-MM-dd" format ``` json "Response": { "StatusCode": 200, "BodyAsJson": { "date": "{{String.Format (DateTime.UtcNow) \"yyyy-MM-dd\"}}" }, "Headers": { "Content-Type": "application/json; charset=utf-8" }, "UseTransformer": true, "TransformerType": "Handlebar" } ``` im getting back wrong date format: ` { "date": "2024-12-20T00:00:00" } ` Tried Scriban transformer too, with same results
adam added the question label 2025-12-29 08:31:51 +01:00
adam closed this issue 2025-12-29 08:31:51 +01:00
Author
Owner

@StefH commented on GitHub (Dec 21, 2024):

It should be "TransformerType": "Handlebars", however Handlebars should be take as default value.

I did test it in a unit test and it seems to work fine. Can you provide the complete mapping and the code how you use it?

@StefH commented on GitHub (Dec 21, 2024): It should be `"TransformerType": "Handlebars"`, however Handlebars should be take as default value. I did test it in a unit test and it seems to work fine. Can you provide the complete mapping and the code how you use it?
Author
Owner

@jusski commented on GitHub (Dec 21, 2024):

Seems like i need to add option:
"TransformerReplaceNodeOptions": "EvaluateAndKeep"
And now it will not evaluate '2024-12-20' string value after transforming as date primitive again
Full example with added 'TransformerReplaceNodeOptions is:

{
  "Request": {
    "Path": {
      "Matchers": [
        {
          "Name": "WildcardMatcher",
          "Pattern": "/test",
          "IgnoreCase": false
        }
      ]
    },
    "Methods": [
      "POST"
    ],
    "HttpVersion": "1.1",
    "Body": {
      "Matcher": {
        "Name": "JsonMatcher",
        "Pattern": {
          "personalCode": "11111"
        },
        "IgnoreCase": true,
        "Regex": false
      }
    }
  },
  "Response": {
    "StatusCode": 200,
    "BodyAsJson": {
      "date": "{{String.Format (DateTime.UtcNow) \"yyyy-MM-dd\"}}"
    },
    "Headers": {
      "Content-Type": "application/json; charset=utf-8"
    },
    "UseTransformer": true,
    "TransformerType": "Handlebar",
    "TransformerReplaceNodeOptions": "EvaluateAndKeep"
  }
}
@jusski commented on GitHub (Dec 21, 2024): Seems like i need to add option: ` "TransformerReplaceNodeOptions": "EvaluateAndKeep"` And now it will not evaluate '2024-12-20' string value after transforming as date primitive again Full example with added 'TransformerReplaceNodeOptions is: ``` json { "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/test", "IgnoreCase": false } ] }, "Methods": [ "POST" ], "HttpVersion": "1.1", "Body": { "Matcher": { "Name": "JsonMatcher", "Pattern": { "personalCode": "11111" }, "IgnoreCase": true, "Regex": false } } }, "Response": { "StatusCode": 200, "BodyAsJson": { "date": "{{String.Format (DateTime.UtcNow) \"yyyy-MM-dd\"}}" }, "Headers": { "Content-Type": "application/json; charset=utf-8" }, "UseTransformer": true, "TransformerType": "Handlebar", "TransformerReplaceNodeOptions": "EvaluateAndKeep" } } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net#652