How to convert date to string value?-VBForums
Results 1 to 3 of 3

Thread: How to convert date to string value?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2004
    Posts
    24

    How to convert date to string value?

    Hi expert,

    I have a data field (birthdate) contains a datetime datatype (mm/dd/yyyy), how can I convert to mmdd??
    say, the birthday is 8/1/1979 and convert to mmdd

    DOB = DS.Tables("AcctInfoTble").Rows(3)("BRTHDATE")
    strResult.Insert(12, DOB.PadLeft(37, " "))

    ---------------------------------------------------------------------------------------------------
    Public Function GenerateA(ByRef strEmpID As String)
    Dim strResult As New System.Text.StringBuilder
    Dim GPConnection As SqlConnection
    Dim dr As SqlDataReader
    Dim GPDataset As New DataSet

    GPConnection = New SqlConnection(".............................................")
    Dim GPDataAdapter As New SqlDataAdapter("SELECT LASTNAME,FRSTNAME,BRTHDATE FROM UPR00102 where EmployID=' " + strEmpID + "'", GPConnection)

    Dim EfundAcct As String
    Dim LName as string
    Dim FName as string
    Dim DOB as string

    GPConnection.Open()
    GPDataAdapter.Fill(GPDataset, "AcctInfoTble")
    Dim DS As DataSet = New DataSet
    LName = DS.Tables("AcctInfoTble").Rows(1).Item("LASTNAME")
    FName = DS.Tables("AcctInfoTble").Rows(2)("LASTNAME")
    DOB = DS.Tables("AcctInfoTble").Rows(3)("BRTHDATE")

    ....................
    'Position 13 16N First Name
    strResult.Insert(12, FName.PadLeft(16, " "))
    ....................
    'Position 37 4A Birthdate
    strResult.Insert(12, DOB.PadLeft(37, " "))

    GPConnection.Dispose()
    Return strResult.ToString
    End Function

  2. #2
    New Member
    Join Date
    Dec 2004
    Posts
    8

    Re: How to convert date to string value?

    Hi,

    You can use the Format command to do this

    Save it from your database as a date

    Dim DOB as Date
    DOB = ....
    Dim newDOB as string
    newDOB = Format (DOB,"mmdd")

    For more formatting options look up the Format function under help index

    Mike

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339

    Re: How to convert date to string value?

    You can also use the native .NET function of the date object called ToString and pass in a format:
    Code:
            Dim d As Date = #7/13/2004#
            MsgBox(d.ToString("MMdd"))
    Case matters in the format string so notice that the Ms are capitalized.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.