URL 中的日期时间格式

发布于 01-20 21:28 字数 743 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

变身佩奇2025-01-27 21:28:40

即使我使用'toString(“ mm/dd/yyyy”)'将trans参数作为字符串作为字符串,但URL总是像这样,

04.%2012.%202022

所以我使用了提供商

ToString("MM/dd/yyyy", new CultureInfo("en-US"))

,但一切都很好。
字符串格式无法超越OS系统设置。

谢谢大家回答我的问题。

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.

榕城若虚2025-01-27 21:28:40

另一种可能的解决方案是在运行项目时更改项目的文化,因此在启动类中您应该添加:

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 (这在大规模时可能会很乏味)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文