Set datetime url format to ISO8601 #1422
-
|
Is there any way to define all datetime format to iso8601? ( |
Beta Was this translation helpful? Give feedback.
Answered by
glennawatson
Jun 16, 2026
Replies: 1 comment
-
|
Two options: Per-parameter via the Query attribute Format property: [Get("/events")]
Task GetEvents([Query(Format = "o")] DateTime from);Global, for every DateTime in URLs - implement IUrlParameterFormatter and set it on RefitSettings: class Iso8601UrlParameterFormatter : DefaultUrlParameterFormatter
{
public override string? Format(object? value, ICustomAttributeProvider attributeProvider, Type type)
{
if (value is DateTime dt)
return dt.ToString("o", CultureInfo.InvariantCulture);
if (value is DateTimeOffset dto)
return dto.ToString("o", CultureInfo.InvariantCulture);
return base.Format(value, attributeProvider, type);
}
}
var settings = new RefitSettings { UrlParameterFormatter = new Iso8601UrlParameterFormatter() };
var api = RestService.For<IMyApi>("https://api.example.com", settings);DefaultUrlParameterFormatter already round-trips IFormattable values with the Format you pass via [Query(Format=...)], so overriding the formatter lets you apply "o" everywhere without annotating each parameter. |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
glennawatson
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Two options:
Per-parameter via the Query attribute Format property:
Global, for every DateTime in URLs - implement IUrlParameterFormatter and set it on RefitSettings: