Abriel
Abriel

Reputation: 583

Changing Only Time in DateTime Format - MVC 4

I've been working with the following Lynda.com tutorial on learning MVC 4 and Razor. I'm stuck on trying to have the time that's displayed only shows the hours, minutes, then AM/PM. As of now, the screen still includes the seconds (as seen below):
enter image description here

I tried formatting my dates like this post about DateTime, which didn't work. Now I have the following code within my function in the controller section entitled "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

This is how Razor is displaying the content from the view "Auction.vbhtml":

    <p>Start Time: @auction.StartTime.ToString() </p>
    <p>End Time: @auction.EndTime.ToString()</p>
    <p>Starting Price: @FormatCurrency(auction.StartPrice.ToString())</p>

Edit(s):
This is how I declared my time variables in my modal file:

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

I've also tried to have it from within the "Auction.vhtml" view the following, which unfortunately gave me the server error indicating the "Input string was not in a correct format.":

<p>Start Time: @auction.StartTime.ToString("g") </p>
    <p>End Time: @auction.EndTime.ToString("g")</p>
    <p>Starting Price: @FormatCurrency(auction.StartPrice.ToString())</p>

What am I doing wrong in either the Razor or MVC code that is not formatting the time? Any help is greatly appreciated!

Upvotes: 0

Views: 8931

Answers (2)

tostringtheory
tostringtheory

Reputation: 877

I would highly suggest looking into the DisplayFormat (MSDN) attribute. You would append it to your models StartTime property like so

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

and then you would output the information in your view by the DisplayFor function:

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

Upvotes: 0

Jacob
Jacob

Reputation: 78920

You should take a look at Custom Date and Time Format Strings on MSDN. Basically, you can pass a format string to the ToString method of your DateTime objects.

Here's a sample that omits the seconds:

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

Upvotes: 6

Related Questions