views:

57

answers:

3

No coffee. Brain. Not. Functioning.

I have this linq query here:

Public Function ListAllVisitDates() As List(Of SelectListItem)
    Dim visitdates = db.SchoolVisitDates.Select(Function(t) New SelectListItem() With {.Text = t.VisitDate, .Value = t.VisitDateID}).ToList()
    Return visitdates
End Function

It returns a long date of MM dd yyyy hh:mm blah blah which I'm populating a dropdown box with. I need it to be a short date of mm/dd/yyy. help?

SOLVED

This was stupid easy. After grabbing the values and creating my list of selectlistitem I just looped through the items and formatted them before passing it into my view:

Dim _VisitDates As New List(Of SelectListItem)
    Try
        _VisitDates = articlerepo.ListAllVisitDates()
        For Each item In _VisitDates
            item.Text = FormatDateTime(item.Text, DateFormat.ShortDate)
        Next
        ViewData("VisitDates") = _VisitDates
    Catch ex As Exception
        Debug.Print(ex.Message)
    End Try
A: 

If it is a DateTime, have you tried using .ToShortDateString()?

NickLarsen
There isn't a command like that. I've tried Format(t.visitdate, vbshortdate) and .ToString("mm/dd/yyyy") but they didn't work :/
keynone
What is the type of `VisitDate`?
NickLarsen
List(Of SelectListItem)
keynone
I mean `t.VisitDate` not the `visitdates` variable.
NickLarsen
That is a DateTime field in my sql db. The values look like this in the db: 2010-11-19 00:00:00.000I just want the 2010-11-19 to be in the selectlist
keynone
+1  A: 
Public Function ListAllVisitDates() As List(Of SelectListItem)
    Dim visitdates = db.SchoolVisitDates.Select(Function(t) New SelectListItem() With {.Text = t.VisitDate.ToString("d"), .Value = t.VisitDateID}).ToList()
    Return visitdates
End Function

Calling ToString("d") on the DateTime will return the date formatted as d/M/yyyy (as would NickLarsen's answer).

MJ Richardson
I gave that a shot, but I keep getting an exception."Conversion from string "d" to type 'Integer' is not valid".I don't know why it's trying to convert it to an integer...I'm calling this function like this:Dim VisitDates As New List(Of SelectListItem)VisitDates = articlerepo.ListAllVisitDates()
keynone
What is the type of the t.VisitDate property? Is it a DateTime or an Integer?
MJ Richardson
It's a DateTime
keynone
A: 

SOLVED

This was stupid easy. After grabbing the values and creating my list of selectlistitem I just looped through the items and formatted them before passing it into my view:

Dim _VisitDates As New List(Of SelectListItem)
Try
    _VisitDates = articlerepo.ListAllVisitDates()
    For Each item In _VisitDates
        item.Text = FormatDateTime(item.Text, DateFormat.ShortDate)
    Next
    ViewData("VisitDates") = _VisitDates
Catch ex As Exception
    Debug.Print(ex.Message)
End Try
keynone