开发者

MVC3 display date + short time without format string

开发者 https://www.devze.com 2023-04-12 16:32 出处:网络
In MVC you can do [DisplayFormat(DataFormatString = \"{0:MM/dd/yyyy h:m开发者_高级运维m tt}\"] This would translate to

In MVC you can do

[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy h:m开发者_高级运维m 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消