URL 中的日期时间格式
我在 Blazor 文件的 EditForm 中有 input 标记(InputDate)。
<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>
然后将其提交到 api 服务器。
但问题是DateTime格式和Url,它显示
https://localhost:44320/api/hotelroom?checkInDate=04.%2012.%202022&checkOutDate=04.%2013.%202022
So the return value is 400错误。
当我用 swagger 检查这个日期时间格式时,没有任何问题。
StartDate 是 Url 中的 checkInDate。
当我尝试调试时,checkInDate 显示为“04”。 11. 2022'。
我预计该日期为 2022 年 4 月 11 日。
我的电脑设置为日本。在不改变本地电脑时间设置的情况下,有什么办法可以解决这个问题吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

发布评论
评论(2)
另一种可能的解决方案是在运行项目时更改项目的文化,因此在启动类中您应该添加:
app.UseRequestLocalization(options =>
{
var supportedCultures = new[] { "en-US" };
options.AddSupportedCultures(supportedCultures)
.AddSupportedUICultures(supportedCultures)
.SetDefaultCulture("en-US");
});
使用类似的提供程序
ToString("MM/dd/yyyy", new CultureInfo("en-US"))
将强制您使用新的文化信息设置每个 ToString (这在大规模时可能会很乏味)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
即使我使用'toString(“ mm/dd/yyyy”)'将trans参数作为字符串作为字符串,但URL总是像这样,
所以我使用了提供商
,但一切都很好。
字符串格式无法超越OS系统设置。
谢谢大家回答我的问题。
Even though I used 'ToString("MM/dd/yyyy")' method to trans parameter as string, the url always was like this
So I used provider
Then everything is good.
string format can't surpass OS system setting.
Thank you all guys replied to my question.