Results 1 to 4 of 4

Thread: [RESOLVED] Date Parsing Problems

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2016
    Posts
    19

    Resolved [RESOLVED] Date Parsing Problems

    I'm sure I'm missing something that one of you will immediately notice, but this is driving me nuts.
    I'm trying to determine the date a file was deleted and placed 'in the recycle bin', and then compare it to 'now' to see if it has been there for over 30 days. I've gotten stuck on parsing the date, so I created a new solution with a single form, and a single button on that form. The below properly shows the deleted date as a string. If I try to dim it as date not string, it fails. If I try to convert.todatetime, it fails. If I try to date.parse, it fails. So I went to date.parseexact, which should always work as long as I use the proper format, but it too fails. Lil' help please.

    Code:
    Imports System.Globalization
    Imports System.IO
    
    
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Dim Shl As New Shell32.Shell
            Dim Recycler As Shell32.Folder = Shl.NameSpace(10)
            Dim FI = Recycler.Items().Item()
    
            Try
                For Each FI In Recycler.Items
                    Dim provider As CultureInfo = CultureInfo.InvariantCulture
                    Dim FileName As String = Recycler.GetDetailsOf(FI, 0)
                    Dim FilePath As String = Recycler.GetDetailsOf(FI, 1)
                    Dim RecycleDate As String = Recycler.GetDetailsOf(FI, 2)
                    'Dim recycledate2 As Date = Recycler.GetDetailsOf(FI, 2) 'Fails.
                    Dim FullPath As String = Recycler.GetDetailsOf(FI, 189)
    
                    Dim DemoDate As String = "8/1/2016 1:58 PM"
                    Dim format As String = "M/d/yyyy h:mm tt" 'same format for Demo and Recycle
    
                    Dim OtherFileDate As String = File.GetCreationTime(Application.ExecutablePath)
                    Dim otherFormat As String = "M/d/yyyy h:mm:ss tt"
    
    
                    MsgBox("Demo: " & DemoDate & vbNewLine &
                           "Other: " & OtherFileDate & vbNewLine &
                           "Recycle: " & RecycleDate,
                           MsgBoxStyle.ApplicationModal,
                           "Test Dates in Order")
    
                    'Dim converted1 As Date = RecycleDate 'Fails. (obviously)
                    'Dim converted2 As Date = Date.Parse(RecycleDate) 'Fails.
    
                    Dim converted3 As Date = Date.ParseExact(DemoDate, format, provider) 'Succeeds!
                    MsgBox(DemoDate & " Converted to: " & converted3, MsgBoxStyle.ApplicationModal, "Converted Demo Date")
    
                    Dim converted31 As Date = Date.ParseExact(OtherFileDate, otherFormat, provider) 'Succeeds!
                    MsgBox(OtherFileDate & " Converted to: " & converted31, MsgBoxStyle.ApplicationModal, "Converted Other Date")
    
                    Dim converted4 As Date = Date.ParseExact(RecycleDate, format, provider) 'Fails.
                    MsgBox(RecycleDate & " Converted to: " & converted4, MsgBoxStyle.ApplicationModal, "Converted Recycle Date")
    
    
                Next
    
            Catch ex As Exception
                MsgBox(ex.ToString)
                Close()
            End Try
    
    
        End Sub
    End Class

    Can someone explain to me why converted 4 fails?

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Date Parsing Problems

    You are not going crazy. Here's the problem. There are unprintable unicode characters in the "RecycleDate" string. In the debugger, break on it, open the value in the editor, copy-paste it and then move your cursor through the text with the arrow keys. You'll notice spots where you clearly press the key, but the cursor doesn't advance. This is what's tripping up the Parse function. Here's a quick fix to elegantly strip all that crud out:

    Code:
    Imports System.Text.RegularExpressions
    
    
    RecycleDate = Regex.Replace(RecycleDate, "[^\u0000-\u007F]", String.Empty)
    Hope that restores some of your sanity!
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2016
    Posts
    19

    Re: Date Parsing Problems

    YOU! YOU!!!! YOU ARE THE BEST!

    I am admittedly not well versed in much of programming. Jack of all trades master of none type. I've never even used the debugger properly until today, and I only know that now that you led me to it!(No idea about break points etc.) I assumed there were 'hidden' characters, but was unsure of how to detect them and didn't even want to assume my programming was correct in itself. Your 2 lines saved my sanity!

    I would like to ask a few more questions of I may:
    What made this date different from the others?
    Why is it unicode when the rest are not?
    How did you know it was unicode?
    What is [^\u0000-\u007F] ?

    I know that's a lot, sorry. But thank you so much for your accurate and concise answer. I GREATLY appreciate it!

  4. #4
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [RESOLVED] Date Parsing Problems

    No problem! To answer your questions:

    I don't know what made the date different. Probably because it was returned from Shell32, and Shell32 is providing that data as a string with some really weird additional characters.
    I'm sure the rest is Unicode, since Windows internally uses Unicode these days for everything , and those other strings returned by Shell32 may have some hidden characters in them as well, but they just don't matter for what you're using them for. How did I know? I didn't until I tried the Regex line and saw it worked.

    Regex is Regular Expression processing, which is like voodoo magic for text strings. You can give it a pattern to look for and have it process it. In the above case, I told it to look in the string "RecycleDate" for any of the Unicode characters that are NOT #0000 through #007F. The "NOT" flag is "^", "\u" signifies Unicode. and the rest is the range. Unicode 00 through 7F are essentially the same as the ASCII table (0 through 127, they did this on purpose for compatibility. http://unicode-table.com/en/#control-character). Anything it finds outside this range, replace with a String.Empty; i.e. remove it. Thus, that line reads: "Look in the string RecycleDate, and if you find any characters that aren't in the ASCII 0-127 range, remove it."

    You can do some seriously fancy string manipulation with Regular Expressions. The only downside is, the filter patterns can get pretty cryptic looking.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

Tags for this Thread

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