2015-06-20 59 views
1

我有以下在我的模型領域:AM/PM不顯示

[DataType(DataType.Time)] 
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm:ss tt}")] 
public DateTime TimeFrom { get; set; } 

在我看來,下面的代碼:

@Html.DisplayFor(modelItem => item.TimeFrom) 

但是當我啓動應用程序的所有它展現的是例如1點00分零零秒

爲什麼它不顯示AM/PM?

回答

1

從這個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秒」你的第一個建議的

+0

結果:你的第二個建議的12:00:00結果:formattedString = 「{0:12:00:00}」 – Palmi

+0

看到我更新的測試我爲你跑。 –

+0

嗨馬丁。感謝您的測試。但是我的輸出沒有PM。這可能是因爲文化信息? – Palmi