locked
Date Format of a Textbox-VB.NET RRS feed

  • Question

  • User-1578974752 posted

    I have a text box which will return value from the select command.

        cmd.CommandText = "select * from studtab where NUMBER = '" + numtxt.Text + "'"
               
                cmd.CommandType = CommandType.Text
                Dim dr As OracleDataReader = cmd.ExecuteReader()
                dr.Read()
               
                duedate.Text = dr.Item("SUP_DATE")

    THE DATE showing is 10/10/2018 1:33:26 PM

    i Want it to be formatted as DD-MON-YYYY and show in duedate.Text
    Appreciate the help

    Wednesday, January 23, 2019 3:41 AM

Answers

  • User541108374 posted

    Hi,

    use .ToString("dd MMM yyyy").

    For example:

    var now = DateTime.Now;
    string date = now.ToString("dd MMM yyyy");

    output:

    23 Jan 2019

    As a sidenote: your sql expression doesn't look safe against sql injection. Try to make use of parameters when making that one up.

    Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2019 7:07 AM
  • User-943250815 posted

    shsu,

    dr.Item("DUE_DATE") should be in DateTime format, if it is in string format first convert to datetime then use ToString.

    Dim MyDateStr as string = "2018-10-13T08:33:18.236-03:00"
    Dim MyDate as DateTime = DateTime.Parse(MyDateStr)
    Dim MyDateFmt as string = MyDate.ToString("dd MMM yyyy")

    https://docs.microsoft.com/en-us/dotnet/api/system.datetime.parse?view=netframework-4.7.2

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2019 12:35 PM

All replies

  • User541108374 posted

    Hi,

    use .ToString("dd MMM yyyy").

    For example:

    var now = DateTime.Now;
    string date = now.ToString("dd MMM yyyy");

    output:

    23 Jan 2019

    As a sidenote: your sql expression doesn't look safe against sql injection. Try to make use of parameters when making that one up.

    Kris.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2019 7:07 AM
  • User-1578974752 posted

    duedate.Text = dr.Item("DUE_DATE").ToString("dd MMM yyyy")

    SHOWING Error

    Unable to cast object of type 'System.String' to type 'System.IFormatProvider'.

    Wednesday, January 23, 2019 10:58 AM
  • User-943250815 posted

    shsu,

    dr.Item("DUE_DATE") should be in DateTime format, if it is in string format first convert to datetime then use ToString.

    Dim MyDateStr as string = "2018-10-13T08:33:18.236-03:00"
    Dim MyDate as DateTime = DateTime.Parse(MyDateStr)
    Dim MyDateFmt as string = MyDate.ToString("dd MMM yyyy")

    https://docs.microsoft.com/en-us/dotnet/api/system.datetime.parse?view=netframework-4.7.2

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 23, 2019 12:35 PM