MVC3显示日期+没有格式字符串的短时间

发布于 2024-12-09 16:25:29 字数 441 浏览 1 评论 0原文

在 MVC 中你可以这样做

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy h:mm tt}"]

这会转化为

2011年10月12日下午1:30

这对于 en-US 用户来说是正确的

,但是假设瑞典用户访问该网站 (sv-SE)

那么我想要

2011-10-12 13:30

所以我不能使用格式字符串..如果我从模型中删除格式属性,我将得到

10/12/2011 1:30:20 PM for en-US 和 2011-10-12 13:30:20 for sv-SE

如何在没有秒的情况下获得正确的格式?

In MVC you can do

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy h:mm tt}"]

This would translate to

10/12/2011 1:30 PM

Which is correct for a en-US user

But lets say a Swedish user visits the site (sv-SE)

Then I want

2011-10-12 13:30

so i cant use format string.. if i remove format attribute from the model I'll get

10/12/2011 1:30:20 PM for en-US
and
2011-10-12 13:30:20 for sv-SE

How can I get the correct format without seconds?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

娇纵 2024-12-16 16:25:29

您可以使用 g 格式说明符,它是“常规日期短时间”格式说明符,它恰好是您正在寻找的格式说明符。

像这样:

 [DisplayFormat(DataFormatString = "{0:g}")]
 // Displays 2011-10-12 11:40 for sv-SE
 // Displays 10/12/2011 11:40 AM for en-US

完整文档:http://msdn.microsoft.com/en-我们/library/az4se3k1.aspx

You can use the g format specifier, which is the "General date short time" format specifier, which happens to be the one you are looking for.

Like this:

 [DisplayFormat(DataFormatString = "{0:g}")]
 // Displays 2011-10-12 11:40 for sv-SE
 // Displays 10/12/2011 11:40 AM for en-US

Full docs: http://msdn.microsoft.com/en-us/library/az4se3k1.aspx

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