【问题标题】:MVC3 display date + short time without format stringMVC3显示日期+短时间无格式字符串
【发布时间】:2023-04-06 12:40:02
【问题描述】:

在 MVC 中你可以这样做

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

这将转化为

2011 年 10 月 12 日下午 1:30

这对于美国用户来说是正确的

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

那我想要

2011-10-12 13:30

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

2011 年 10 月 12 日下午 1:30:20(美国) 和 2011-10-12 13:30:20 for sv-SE

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

【问题讨论】:

    标签: asp.net-mvc-3 localization culture datetime-format


    【解决方案1】:

    您可以使用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-us/library/az4se3k1.aspx

    【讨论】:

    • 现在我遇到了 DisplayFormat 属性和自定义编辑器的问题,但是一旦我让它工作,你的解决方案可能会工作。谢谢!
    猜你喜欢
    • 2015-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 2012-05-18
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多