|
-
Aug 2nd, 2016, 01:17 PM
#1
Thread Starter
Junior Member
[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?
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|