locked
Need help reformatting a date to show the name of the month RRS feed

  • Question

  • User-1682007629 posted

     Hello, I am reading in a date from the database in the format
     "7/23/2059 12:00:00 AM"


     Is it possible to reformat this string as follows where the full name
     of the month is shown. Then the day followed by a comma and the year?


     "August 5, 2009"


     I could write my own code to do this, but I don't want to reinvent the wheel. If someone
     could help me out I would appreciate it.

     Here is my code I use to read the value. Basically I am getting it from a data set, and the date value
     is of type dateTime

                  Dim tempString As String
                  tempString = ds.Tables("employee").Rows(0).Item("dateOfEmployeeExpire").ToString().Trim()

    Monday, August 10, 2009 5:27 PM

Answers

  • User-151304380 posted
    Below is an example for you to modify to your usage:
    Module DateToStringExample
       Public Sub Main()
          Dim dateValue As Date = #6/15/2008 9:15:07PM#
          ' Create an array of standard format strings.
          Dim standardFmts() As String = {"d", "D", "f", "F", "g", "G", _
                                          "m", "o", "R", "s", "t", "T", _
                                          "u", "U", "y"}
          ' Output date and time using each standard format string.
          For Each standardFmt As String In standardFmts
             Console.WriteLine("{0}: {1}", standardFmt, _
                               dateValue.ToString(standardFmt))
          Next
          Console.WriteLine()
    
          ' Create an array of some custom format strings.
          Dim customFmts() As String = {"h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f", _
                                        "dd MMM HH:mm:ss", "\Mon\t\h\: M", "HH:mm:ss.ffffzzz" }
          ' Output date and time using each custom format string.
          For Each customFmt As String In customFmts
             Console.WriteLine("'{0}': {1}", customFmt, _
                               dateValue.ToString(customFmt))
          Next
       End Sub
    End Module
    ' This example displays the following output to the console:
    '       d: 6/15/2008
    '       D: Sunday, June 15, 2008
    '       f: Sunday, June 15, 2008 9:15 PM
    '       F: Sunday, June 15, 2008 9:15:07 PM
    '       g: 6/15/2008 9:15 PM
    '       G: 6/15/2008 9:15:07 PM
    '       m: June 15
    '       o: 2008-06-15T21:15:07.0000000
    '       R: Sun, 15 Jun 2008 21:15:07 GMT
    '       s: 2008-06-15T21:15:07
    '       t: 9:15 PM
    '       T: 9:15:07 PM
    '       u: 2008-06-15 21:15:07Z
    '       U: Monday, June 16, 2008 4:15:07 AM
    '       y: June, 2008
    '       
    '       'h:mm:ss.ff t': 9:15:07.00 P
    '       'd MMM yyyy': 15 Jun 2008
    '       'HH:mm:ss.f': 21:15:07.0
    '       'dd MMM HH:mm:ss': 15 Jun 21:15:07
    '       '\Mon\t\h\: M': Month: 6
    '       'HH:mm:ss.ffffzzz': 21:15:07.0000-07:00
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 10, 2009 6:06 PM
  • User-1567482218 posted

    You can also consider fetching the formatted value directly from SQL Server, using this query -

    select DATENAME(month, getdate()) + ' ' + DATENAME(d, getdate()) + ', ' + DATENAME(yyyy, getdate())

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 11, 2009 1:14 AM

All replies

  • User-151304380 posted
    Below is an example for you to modify to your usage:
    Module DateToStringExample
       Public Sub Main()
          Dim dateValue As Date = #6/15/2008 9:15:07PM#
          ' Create an array of standard format strings.
          Dim standardFmts() As String = {"d", "D", "f", "F", "g", "G", _
                                          "m", "o", "R", "s", "t", "T", _
                                          "u", "U", "y"}
          ' Output date and time using each standard format string.
          For Each standardFmt As String In standardFmts
             Console.WriteLine("{0}: {1}", standardFmt, _
                               dateValue.ToString(standardFmt))
          Next
          Console.WriteLine()
    
          ' Create an array of some custom format strings.
          Dim customFmts() As String = {"h:mm:ss.ff t", "d MMM yyyy", "HH:mm:ss.f", _
                                        "dd MMM HH:mm:ss", "\Mon\t\h\: M", "HH:mm:ss.ffffzzz" }
          ' Output date and time using each custom format string.
          For Each customFmt As String In customFmts
             Console.WriteLine("'{0}': {1}", customFmt, _
                               dateValue.ToString(customFmt))
          Next
       End Sub
    End Module
    ' This example displays the following output to the console:
    '       d: 6/15/2008
    '       D: Sunday, June 15, 2008
    '       f: Sunday, June 15, 2008 9:15 PM
    '       F: Sunday, June 15, 2008 9:15:07 PM
    '       g: 6/15/2008 9:15 PM
    '       G: 6/15/2008 9:15:07 PM
    '       m: June 15
    '       o: 2008-06-15T21:15:07.0000000
    '       R: Sun, 15 Jun 2008 21:15:07 GMT
    '       s: 2008-06-15T21:15:07
    '       t: 9:15 PM
    '       T: 9:15:07 PM
    '       u: 2008-06-15 21:15:07Z
    '       U: Monday, June 16, 2008 4:15:07 AM
    '       y: June, 2008
    '       
    '       'h:mm:ss.ff t': 9:15:07.00 P
    '       'd MMM yyyy': 15 Jun 2008
    '       'HH:mm:ss.f': 21:15:07.0
    '       'dd MMM HH:mm:ss': 15 Jun 21:15:07
    '       '\Mon\t\h\: M': Month: 6
    '       'HH:mm:ss.ffffzzz': 21:15:07.0000-07:00
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 10, 2009 6:06 PM
  • User-1682007629 posted

     Cool I got it:

    Dim tempString As String
                    tempString = ds.Tables("employee").Rows(0).Item("dateOfEmployeeExpire").ToString().Trim()
                    Dim FormatedDate As String = DateTime.Parse(tempString).ToString("MMMM dd, yyyy").ToString()

    Monday, August 10, 2009 6:25 PM
  • User-151304380 posted

    Don't forget to set your post to "resolved" if your want to thank me.

    Monday, August 10, 2009 6:35 PM
  • User-1567482218 posted

    You can also consider fetching the formatted value directly from SQL Server, using this query -

    select DATENAME(month, getdate()) + ' ' + DATENAME(d, getdate()) + ', ' + DATENAME(yyyy, getdate())

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 11, 2009 1:14 AM
  • User-1682007629 posted

     You bet, thanks again!

    Tuesday, August 11, 2009 10:29 AM