Results 1 to 9 of 9

Thread: [RESOLVED] convert text to dd/mm/yyyy format

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    8

    Resolved [RESOLVED] convert text to dd/mm/yyyy format

    I'm trying to convert the "29/09/2009" string to date format dd/mm/yyyy

    I'm using visual studio 2008, and i've tried the following:

    dim sdate as string
    sdate = "29/09/2009"

    mydate = DateTime.Parse(sDate, Globalization.CultureInfo.CreateSpecificCulture("el-GR"))

    and

    mydate = Date.Parse(sDate).ToShortDateString()

    and i always get #9/29/2009# instead of #29/09/2009# that i want

    Any help please?
    Last edited by tkoletsis; Sep 29th, 2009 at 06:13 AM.

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: convert text to dd/mm/yyyy format

    Hey,

    You can create a string representation of a DateTime object using String.Format:

    http://msdn.microsoft.com/en-us/library/fht0f5be.aspx

    Scroll to the bottom of that page, there are lots of examples!

    Hope that helps!!

    Gary

  3. #3
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: convert text to dd/mm/yyyy format

    You can't just do this?
    Code:
    Dim myDate As New Date("29/09/2009")
    myDate.ToString("dd/MM/yyyy")
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: convert text to dd/mm/yyyy format

    Quote Originally Posted by tkoletsis View Post
    I'm trying to convert the "29/09/2009" string to date format dd/mm/yyyy

    I'm using visual studio 2008, and i've tried the following:

    dim sdate as string
    sdate = "29/09/2009"

    mydate = DateTime.Parse(sDate, Globalization.CultureInfo.CreateSpecificCulture("el-GR"))

    and

    mydate = Date.Parse(sDate).ToShortDateString()

    and i always get #9/29/2009# instead of #29/09/2009# that i want

    Any help please?
    A Date is a Date is a Date. Dates don't have formats because they are binary values. Only string representations of dates have format. When you see #9/29/2009# that does NOT mean that your Date has that specific format. That's simply how VS represents Date literals. The value of the date is the value of the date.

    Consider this. If you were to write this code:
    vb.net Code:
    1. Dim number As Integer = &HFF
    and the you check the value of the variable, VS would tell you that the value was 255. Does that mean that the number has a decimal format instead of a hexadecimal format? Of course not. The number is stored in the computer in binary form, as is everything. 255 is just the way that VS represents the numeric literal.

    Dates are the same. A Date is just a number stored in the computer in binary form. The number represents a number of milliseconds since a specific point in time. How can that have any format? A Date is simply a value that represents a point in time. Format is ONLY an issue when you want to display that value, at which point you need to convert it to a string, at which point you need to decide what format to use.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    8

    Re: convert text to dd/mm/yyyy format

    Ok i agree, but i still have problem with the following code:

    Dim rows As DataRow() = dt.Select("[DateIn] = #" & tmpDate & "#")

    I get that exception there:

    System.FormatException was unhandled
    Message="String was not recognized as a valid DateTime."
    Source="mscorlib"
    StackTrace:
    at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.Data.ConstNode..ctor(DataTable table, ValueType type, Object constant, Boolean fParseQuotes) at System.Data.ExpressionParser.Parse() at System.Data.DataExpression..ctor(DataTable table, String expression, Type type) at System.Data.Select..ctor(DataTable table, String filterExpression, String sort, DataViewRowState recordStates) at System.Data.DataTable.Select(String filterExpression)

    etc...

    tmpDate has the value: #2/13/2009#
    Datein has the value: #2/13/2009#

    My regional settings have the format dd/mm/yyyy

    What can I do?


    Quote Originally Posted by jmcilhinney View Post
    A Date is a Date is a Date. Dates don't have formats because they are binary values. Only string representations of dates have format. When you see #9/29/2009# that does NOT mean that your Date has that specific format. That's simply how VS represents Date literals. The value of the date is the value of the date.

    Consider this. If you were to write this code:
    vb.net Code:
    1. Dim number As Integer = &HFF
    and the you check the value of the variable, VS would tell you that the value was 255. Does that mean that the number has a decimal format instead of a hexadecimal format? Of course not. The number is stored in the computer in binary form, as is everything. 255 is just the way that VS represents the numeric literal.

    Dates are the same. A Date is just a number stored in the computer in binary form. The number represents a number of milliseconds since a specific point in time. How can that have any format? A Date is simply a value that represents a point in time. Format is ONLY an issue when you want to display that value, at which point you need to convert it to a string, at which point you need to decide what format to use.
    Last edited by tkoletsis; Sep 30th, 2009 at 08:19 AM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: convert text to dd/mm/yyyy format

    As I said, Date values themselves don't have a format but when you convert a Date to a String or vice versa, THAT is when format is important. In your case, when you use the DataTable.Select method you MUST specify dates in M/dd/yyyy format. As such, you can do this:
    vb.net Code:
    1. Dim rows As DataRow() = dt.Select("[DateIn] = #" & tmpDate.ToString("M/dd/yyyy") & "#")
    or my preference would be:
    vb.net Code:
    1. Dim rows As DataRow() = dt.Select(String.Format("[DateIn] = #{0:M/dd/yyyy}#", tmpDate))
    Hopefully this shows why you need to provide the full story when you first post. If we know exactly what you're trying to do then we can provide the exact solution. If you only provide half the story then you'll likely only get half answers. WHY you're trying to do what you're trying to do is usually just as important as WHAT you're trying to do.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2009
    Posts
    8

    Re: convert text to dd/mm/yyyy format

    Thank you, your help was very important

    Quote Originally Posted by jmcilhinney View Post
    As I said, Date values themselves don't have a format but when you convert a Date to a String or vice versa, THAT is when format is important. In your case, when you use the DataTable.Select method you MUST specify dates in M/dd/yyyy format. As such, you can do this:
    vb.net Code:
    1. Dim rows As DataRow() = dt.Select("[DateIn] = #" & tmpDate.ToString("M/dd/yyyy") & "#")
    or my preference would be:
    vb.net Code:
    1. Dim rows As DataRow() = dt.Select(String.Format("[DateIn] = #{0:M/dd/yyyy}#", tmpDate))
    Hopefully this shows why you need to provide the full story when you first post. If we know exactly what you're trying to do then we can provide the exact solution. If you only provide half the story then you'll likely only get half answers. WHY you're trying to do what you're trying to do is usually just as important as WHAT you're trying to do.

  8. #8
    New Member
    Join Date
    Sep 2010
    Posts
    1

    Re: convert text to dd/mm/yyyy format

    Hi there i have the following code for a hotel management system and whenever i execute the program i get the following error

    String was not recognized as a valid Date Time.

    Can you help????

    Code:
    Private Sub txtDays_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDays.TextChanged
            Dim tmpDate As Date
    
            tmpDate = dtpDateIn.Value
    
            If txtAdults.Tag = "" Then Exit Sub
    
            ExecNonQuery("DELETE [DateIn] " & "FROM [Rate_Per_Period] " & "WHERE [DateIn]>#" & dtpDateOut.Value.AddDays(-1) & "#")
    
            Dim intAdults As Short
    
            If txtAdults.Text = "" Then Exit Sub
    
            If CDbl(txtAdults.Text) = hsAdults.Minimum Then
                intAdults = 0
            Else
                intAdults = CShort(txtAdults.Text) - hsAdults.Minimum
            End If
    
            Dim qry As String = "SELECT * FROM Rate_Per_Period WHERE FolioNumber = '" & txtFolioNumber.Text & "' ORDER BY DateIn"
    
            Dim cnHotel As OleDbConnection = New OleDbConnection(cnString)
    
            Try
                Dim da As New OleDbDataAdapter()
                da.SelectCommand = New OleDbCommand(qry, cnHotel)
    
                Dim cb As OleDbCommandBuilder = New OleDbCommandBuilder(da)
    
                Dim ds As New DataSet()
                da.Fill(ds, "Rate_Per_Period")
    
                Dim dt As DataTable = ds.Tables("Rate_Per_Period")
    
                Dim newRow As DataRow
    
                Do Until tmpDate > dtpDateOut.Value.AddDays(-1)
                    Dim rows As DataRow() = dt.Select("[DateIn] = #" & tmpDate & "#")
                   
                    If rows.Count = 0 Then
                        newRow = dt.NewRow()
    
                        newRow("FolioNumber") = txtFolioNumber.Text
                        newRow("DateIn") = tmpDate
                        newRow("RoomNumber") = txtRoomNumber.Text
                        newRow("RateTypeID") = cboRateType.SelectedValue
                        newRow("Rate") = txtRate.Text
                        newRow("Adults") = CDbl(txtAdults.Tag) * intAdults
                        newRow("Childrens") = CDbl(toMoney(txtChildrens.Tag)) * toNumber(txtChildrens.Text)
    
                        dt.Rows.Add(newRow)
                    End If
                    tmpDate = System.DateTime.FromOADate(tmpDate.ToOADate + 1)
                Loop
    
                da.Update(ds, "Rate_Per_Period")
            Catch ex As OleDbException
                MsgBox(ex.ToString)
            Finally
                cnHotel.Close()
            End Try
        End Sub

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: convert text to dd/mm/yyyy format

    Quote Originally Posted by dgichane View Post
    Hi there i have the following code for a hotel management system and whenever i execute the program i get the following error

    String was not recognized as a valid Date Time.

    Can you help????
    If you're going to post to an existing thread then you should at least read it first. Post #6 addresses that specifically, so the answer is already there and you simply ignored it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Click Here to Expand Forum to Full Width