skip to Main Content

In Visual Basic, I have an object with an ItemDate key in it with the value being assigned to a label in ASPX. Here is the code:

lblItemDate.Text = .ItemDate

The result on the front end is ‘2021/11/15’. I want the result to be: ‘November 15, 2021’

What do I need to do in Visual Basic to make the result on the front end be ‘November 15, 2021’ instead of ‘2021/11/15’?

In another VB file, the ItemDate object key is created this way:

oItem.ItemDate = Trim(odbcReader("ItemDate").ToString)

2

Answers


  1. Assuming you really have a .Net DateTime struct:

    lblItemDate.Text = .ItemDate.ToString("MMMM dd, yyyy")
    

    Otherwise you have a string, in which case you want to parse into a .Net DateTime struct so you can use the same ToString() call above:

    Dim MyDate As DateTime = DateTime.ParseExact( .ItemDate, "yyyy/MM/dd") 
    lblItemDate.Text = MyDate.ToString("MMMM dd, yyyy")
    

    Even better if you can update your code so ItemDate is a DateTime value in the first place, and the Parse() call is moved to the point where the object is first created.

    It’s been a while, so I don’t recall whether the .ItemDate shortcut is available in the context of a function call. You may need to use the full version of the variable name.

    Login or Signup to reply.
  2. Besides the .Net functions mentioned in another answer, which work across different languages, there is also the Format(..) function traditionally included with professional implementations of BASIC since the early 70’s (I first used it in DEC’s BASIC-Plus in 1976).

    To get ‘November 15, 2021’ you’d do it like this:

    lblItemDate.Text = Format(.ItemDate, "MMMM d, yyyy")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search