'datetime format in URL
I have input tag(InputDate) in EditForm in Blazor file.
<div class="form-group">
<label>Check In Date</label>
<InputDate @bind-Value="HomeModel.StartDate" class="form-control" min="@DateTime.Now.ToString("MM/dd/yyyy")" type="text"/>
</div>
then submit this to api server.
But problem is DateTime format with Url, it shows
https://localhost:44320/api/hotelroom?checkInDate=04.%2012.%202022&checkOutDate=04.%2013.%202022
So the return value is 400 error.
When I check this datetime format with swagger, nothing problem.
StartDate is checkInDate in Url.
when I try to debug, checkInDate shows like '04. 11. 2022'.
I expect this as 04/11/2022.
My pc is set for Japan. Without changing local pc's time setting, is there any way to fix this?
Solution 1:[1]
Even though I used 'ToString("MM/dd/yyyy")' method to trans parameter as string, the url always was like this
04.%2012.%202022
So I used provider
ToString("MM/dd/yyyy", new CultureInfo("en-US"))
Then everything is good. string format can't surpass OS system setting.
Thank you all guys replied to my question.
Solution 2:[2]
Another possible solution it's changing the culture of the project when you run it, so in your startup class you should add:
app.UseRequestLocalization(options =>
{
var supportedCultures = new[] { "en-US" };
options.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures)
.SetDefaultCulture("en-US");
});
Using provider like
ToString("MM/dd/yyyy", new CultureInfo("en-US"))
Will force you to set every ToString with the new culture info (which can be tedious on a large scale)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sayditto |
| Solution 2 | Leandro Toloza |
