Results 1 to 3 of 3

Thread: DateTime.Parse Problem

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2007
    Posts
    258

    DateTime.Parse Problem

    Alright, well this code has been working fine for a bit now, I guess since I was selecting double digit numbers in my Month Calendar but now since its in the Single Digits I'm getting a Error and I know why.

    vb Code:
    1. Dim testStr As String = DRow.Item("Date (D/M/Y)").ToString()
    2.             Dim sr As New SelectionRange()
    3.             sr.Start = DateTime.Parse(testStr.Substring(testStr.IndexOf("Start: ") + 7, 10))
    4.             sr.End = DateTime.Parse(testStr.Substring(testStr.IndexOf("End: ") + 5, 10))

    Where +7 and +5 are right beside it ", 10" thats where my error is. 10 is for Double Digits while 9 is for Single Digits, is it possible for me to figure out this way in a different matter? Heres 2 Examples:

    Start: 10/8/2008 End: 10/14/2008 < works with , 10 and nine but only grabs 200 and not the 8
    Start: 10/1/2008 End: 10/4/2008 < works with , 9

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: DateTime.Parse Problem

    You split the string by "End:", then work on individual string of the array. Something like this:
    Code:
    Dim s As String = "Start: 10/8/2008 End: 10/14/2008"
    Dim parts() As String = s.Split("End:")
    Dim startDate As Date = Date.Parse(parts(0).Replace("Start:", "").Trim())
    Dim endDate As Date = Date.Parse(parts(1).Trim())
    And of course, you need to implement errror handling to your code, like using Date.TryParse instead of Date.Parse.
    Since you know the format of the date, you can also use Date.TryParseExact and pass in the date format.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: DateTime.Parse Problem

    Code:
            Dim s As String = "Start:  10/8/2008 End:  10/14/2008"
            Dim parts() As String
            s = s.Replace("  ", " ")
            parts = s.Split(" "c)
            Dim startDate As Date = Date.Parse(parts(1))
            Dim endDate As Date = Date.Parse(parts(3))
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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