Abr*_*iel 0 asp.net datetime razor asp.net-mvc-4
我一直在使用以下关于学习MVC 4和Razor的Lynda.com教程.我一直试图让显示的时间只显示小时,分钟,然后是上午/下午.截至目前,屏幕仍包含秒数(如下所示):

我尝试格式化日期,就像这篇关于DateTime的帖子一样,但是没有用.现在我在我的函数中的名为"AuctionsController.vb"的控制器部分中有以下代码,类似于这篇文章:
Function Auction() As ActionResult
Dim mainauction = New MVCAuction3.Models.Auctions
Dim ts As New TimeSpan(10, 0, 0)
mainauction.Title = "Example Auction"
mainauction.Description = "This is an example Auction"
mainauction.StartTime = DateTime.Now + ts
mainauction.EndTime = DateTime.Now.AddDays(7) + ts
mainauction.StartPrice = 1.0
mainauction.CurrentPrice = Nothing
ViewData("Auction") = mainauction
Return View()
End Function
Run Code Online (Sandbox Code Playgroud)
这就是Razor如何显示"Auction.vbhtml"视图中的内容:
<p>Start Time: @auction.StartTime.ToString() </p>
<p>End Time: @auction.EndTime.ToString()</p>
<p>Starting Price: @FormatCurrency(auction.StartPrice.ToString())</p>
Run Code Online (Sandbox Code Playgroud)
编辑:
这是我在模态文件中声明我的时间变量的方式:
Private Property x_StartTime As DateTime
Private Property x_EndTime As DateTime
Public Property StartTime() As DateTime
Get
Return x_StartTime
End Get
Set(value As DateTime)
x_StartTime = value
End Set
End Property
Public Property EndTime() As DateTime
Get
Return x_EndTime
End Get
Set(value As DateTime)
x_EndTime = value
End Set
End Property
Run Code Online (Sandbox Code Playgroud)
我还尝试从"Auction.vhtml"视图中获取以下内容,不幸的是,它给了我服务器错误,指示"输入字符串格式不正确".:
<p>Start Time: @auction.StartTime.ToString("g") </p>
<p>End Time: @auction.EndTime.ToString("g")</p>
<p>Starting Price: @FormatCurrency(auction.StartPrice.ToString())</p>
Run Code Online (Sandbox Code Playgroud)
在没有格式化时间的Razor或MVC代码中,我做错了什么?任何帮助是极大的赞赏!
您应该查看MSDN 上的自定义日期和时间格式字符串.基本上,您可以将格式字符串传递给对象的ToString方法DateTime.
这是一个省略秒的示例:
auction.StartTime.ToString("M/d/yyyy hh:mm tt")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7693 次 |
| 最近记录: |