0

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!

Community
  • 1
  • 1
Abriel
  • 583
  • 1
  • 10
  • 33
  • I don't see any attempt to format the time in your code? – Ant P Sep 16 '13 at 20:51
  • @AntP I went ahead and updated my question a bit, but I tried to format the string within Razor, but gave me a server error: *"Input string was not in a correct format."* – Abriel Sep 16 '13 at 20:58
  • Aside - watch out for misuse of `DateTime.Now` in a web application. [Read more here](http://codeofmatt.com/2013/04/25/the-case-against-datetime-now/). – Matt Johnson-Pint Sep 16 '13 at 21:45
  • @MattJohnson Thanks for the article! :) After I figure out the solution, will have to switch the `DateTime.Now` – Abriel Sep 16 '13 at 22:00

2 Answers2

6

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")
Jacob
  • 77,566
  • 24
  • 149
  • 228
  • So when I do this, do I do it within the view like the update I made to my question above, like auction.StartTime.ToString("g")? – Abriel Sep 16 '13 at 21:00
  • Yes, you can do it in the view. – Jacob Sep 16 '13 at 21:24
  • Your usage of `"g"` that you added above should also work. Are you sure the "Input string was not in a correct format." error was for those lines? – Jacob Sep 16 '13 at 21:29
  • Oh, just to be sure, is your data type for `StartTime` a `DateTime`? Or is it something else? – Jacob Sep 16 '13 at 21:32
  • Yes my `StartTime` variable within my modal file is a `DateTime`. And I thought the `"g"` would've worked, but gave me the "Imput string was not in a correct format." – Abriel Sep 16 '13 at 21:40
  • somehow this format started working for me after configuring a few things in my file. Thanks a lot for the help! :) – Abriel Sep 17 '13 at 14:03
0

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)
tostringtheory
  • 877
  • 8
  • 19
  • The following above seems to be written in C#. How would the `DisplayFormat... rest of code` fit into the VB form of the `public DateTime StartTime {get;set;}`? – Abriel Sep 16 '13 at 21:06
  • Check out that MSDN article I linked to. If you haven't noticed by now, an important thing to note is that there are tabs on the code previews for the different languages. Check out the one for VB code. I am not a VB person by any means, and I don't have the means right now to come up with the correct VB code and validate it - sorry! – tostringtheory Sep 16 '13 at 21:22