【问题标题】:Changing Only Time in DateTime Format - MVC 4仅更改 DateTime 格式的时间 - MVC 4
【发布时间】:2013-09-21 03:15:22
【问题描述】:

我一直在与以下Lynda.com tutorial 合作学习 MVC 4 和 Razor。我一直试图让显示的时间只显示小时、分钟,然后是 AM/PM。截至目前,屏幕仍包含秒数(如下所示):

我尝试像this post about DateTime 这样格式化我的日期,但没有成功。现在我的函数中有以下代码,位于标题为“AuctionsController.vb”的控制器部分,similar to this post

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

这是 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>

修改:
这就是我在模态文件中声明时间变量的方式:

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

我还尝试从“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>

我在没有格式化时间的 Razor 或 MVC 代码中做错了什么?非常感谢任何帮助!

【问题讨论】:

  • 我没有看到任何尝试在您的代码中格式化时间?
  • @AntP 我继续并更新了我的问题,但我尝试在 Razor 中格式化字符串,但给了我一个服务器错误:“输入字符串的格式不正确。 "
  • 除此之外 - 注意在 Web 应用程序中滥用 DateTime.NowRead more here.
  • @MattJohnson 感谢您的文章! :) 在我弄清楚解决方案后,将不得不切换DateTime.Now

标签: asp.net asp.net-mvc-4 datetime razor


【解决方案1】:

您应该查看 MSDN 上的 Custom Date and Time Format Strings。基本上,您可以将格式字符串传递给您的 DateTime 对象的 ToString 方法。

这是一个省略秒数的示例:

auction.StartTime.ToString("M/d/yyyy hh:mm tt")

【讨论】:

  • 所以当我这样做时,我是否会像我对上面的问题所做的更新一样,在视图中执行它,比如auction.StartTime.ToString("g")?
  • 是的,您可以在视图中进行。
  • 您在上面添加的"g" 的使用也应该有效。您确定“输入字符串的格式不正确”。错误是那些行?
  • 哦,为了确定一下,您的StartTime 的数据类型是DateTime 吗?还是别的什么?
  • 是的,我的模态文件中的StartTime 变量是DateTime。我认为"g" 会起作用,但给了我“输入字符串的格式不正确。”
【解决方案2】:

我强烈建议查看 DisplayFormat (MSDN) 属性。您可以将其附加到您的模型 StartTime 属性中,如下所示

[DisplayFormat(ApplyFormatInEditMode=true, DataFormatString = "{0:M/d/yyyy hh:mm tt}")]
public DateTime StartTime {get;set;}

然后你会通过DisplayFor函数在你的视图中输出信息:

@Html.DisplayFor(x=> x.StartTime)

【讨论】:

  • 以下内容似乎是用 C# 编写的。 DisplayFormat... rest of code 如何适应 public DateTime StartTime {get;set;} 的 VB 形式?
  • 查看我链接到的 MSDN 文章。如果您现在还没有注意到,需要注意的重要一点是不同语言的代码预览上有标签。查看 VB 代码。无论如何,我都不是 VB 人,而且我现在没有办法提出正确的 VB 代码并对其进行验证 - 抱歉!
猜你喜欢
  • 1970-01-01
  • 2021-10-02
  • 2020-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多