从这个MSDN article我把以下内容:
// This example displays the following output to the console:
// d: 6/15/2008
// D: Sunday, June 15, 2008
// f: Sunday, June 15, 2008 9:15 PM
// F: Sunday, June 15, 2008 9:15:07 PM
// g: 6/15/2008 9:15 PM
// G: 6/15/2008 9:15:07 PM
// m: June 15
// o: 2008-06-15T21:15:07.0000000
// R: Sun, 15 Jun 2008 21:15:07 GMT
// s: 2008-06-15T21:15:07
// t: 9:15 PM
// T: 9:15:07 PM
// u: 2008-06-15 21:15:07Z
// U: Monday, June 16, 2008 4:15:07 AM
// y: June, 2008
//
// 'h:mm:ss.ff t': 9:15:07.00 P
// 'd MMM yyyy': 15 Jun 2008
// 'HH:mm:ss.f': 21:15:07.0
// 'dd MMM HH:mm:ss': 15 Jun 21:15:07
// '\Mon\t\h\: M': Month: 6
// 'HH:mm:ss.ffffzzz': 21:15:07.0000-07:00
基于此,它看起来像你只需要以下条件:
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:T}")]
得到“下午9时15分07秒”为输出。警告:我没有测试过这个以确保它能正常工作。
我也不能肯定为什么“TT”你有没有被兑现。您是否尝试将值写入使用该属性的代码以确保其正常工作?
string formattedString = myObject.TimeFrom.ToString("{0:hh:mm:ss tt}");
这可能会帮助您深入了解以确保您拥有正确的格式字符串。
更新
我只是跑在我的日期字段的一个测试,并将其与改变时间字段如下:
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy hh:mm:ss tt}", ApplyFormatInEditMode = true)]
[DataType(DataType.Time)]
public DateTime LastInventory { get; set; }
输出为DateTime.Now是“6月20日/ 2015年下午三时12分33秒”你的第一个建议的
结果:你的第二个建议的12:00:00结果:formattedString = “{0:12:00:00}” – Palmi
看到我更新的测试我为你跑。 –
嗨马丁。感谢您的测试。但是我的输出没有PM。这可能是因为文化信息? – Palmi