file date comparation

  • Thread starter Thread starter JFB
  • Start date Start date
J

JFB

Hi All,
I'm trying to find if a file name that includes the date is before todays to
do some procedures.
In my IF statement is working is just a few dates before but not is a year
before.
How can I fix this?
Tks in advance...
This is what I have:
'Set today's date

Dim todayDay As String

If Month(Now) < 10 Then todayDay = todayDay & "0" & Month(Now) Else todayDay
= todayDay & Month(Now)

If Date.Today.Day < 10 Then todayDay = todayDay & "0" & Date.Today.Day Else
todayDay = todayDay & Date.Today.Day

todayDay = todayDay & CStr(Year(Now).ToString)

'Get Files from Spool Folder

objFolder = dir.GetDirectoryRoot(folderName)

colFiles = dir.GetFiles(folderName)

If colFiles(i).Substring(colFiles(i).LastIndexOf("\") + 1, 8) <= todayDay
Then
 
JFB said:
Hi All,
I'm trying to find if a file name that includes the date is before todays
to do some procedures.
In my IF statement is working is just a few dates before but not is a year
before.
How can I fix this?
Tks in advance...
This is what I have:
'Set today's date

Dim todayDay As String

If Month(Now) < 10 Then todayDay = todayDay & "0" & Month(Now) Else
todayDay = todayDay & Month(Now)

If Date.Today.Day < 10 Then todayDay = todayDay & "0" & Date.Today.Day
Else todayDay = todayDay & Date.Today.Day

todayDay = todayDay & CStr(Year(Now).ToString)

'Get Files from Spool Folder

objFolder = dir.GetDirectoryRoot(folderName)

colFiles = dir.GetFiles(folderName)

If colFiles(i).Substring(colFiles(i).LastIndexOf("\") + 1, 8) <= todayDay
Then

Dim today As String = Now().ToString("yyyymmdd")
Dim fileDate As String = ParseFileDateAndStoreAsYYYYMMDD

If today >= fileDate
...
End If

HTH,
Mythran
 
what is the format of your filename date?

ie. is it file01032003.dat or something?

You can parse it into a date structure and then use simple comparison
operators.
 
Tks for you reply
But I already have the file name as MMDDYYYY
How can I parse the file name?
JFB
 
theDate = Date.Parse ( theString )


JFB said:
Tks for you reply
But I already have the file name as MMDDYYYY
How can I parse the file name?
JFB
 
Robin Tucker said:
what is the format of your filename date?

ie. is it file01032003.dat or something?

You can parse it into a date structure and then use simple comparison
operators.


Not exactly as straight forward as you would think. Comparing a date with
another for less than or equality, you need to not only check the date, but
you have to take into consideration the times.

For example:

Dim date1 As DateTime = DateTime.Parse("06/22/2005")
Dim date2 As DateTime = Now()

date1 is < date2...not equal to date2 :)

Mythran
 
Mythran,
You can use DateTime.Date to "remove" the times:

Dim date1 As DateTime = DateTime.Parse("06/22/2005")
Dim date2 As DateTime = Now()

Debug.WriteLine(date1.Date < date2.Date, "date1 < date2")

Alternatively you can simply use Today() if you want today's date, as
opposed to the current date & time.

Dim date1 As DateTime = DateTime.Parse("06/22/2005")
Dim date2 As DateTime = Today()

Debug.WriteLine(date1 < date2, "date1 < date2")

Conversely you can use DateTime.TimeOfDay to remove the dates.

Dim time1 As TimeSpan = date1.TimeOfDay
Dim time2 As TimeSpan = date2.TimeOfDay

Debug.WriteLine(TimeSpan.Compare(time1, time2) < 0, "time1 < time2")

Unfortunately we need to wait for VS.NET 2005 to use the overloaded <
operator on TimeSpan.

' VS.NET 2005 syntax
Debug.WriteLine(time1 < time2, "time1 < time2")

Hope this helps
Jay

|
| message | > what is the format of your filename date?
| >
| > ie. is it file01032003.dat or something?
| >
| > You can parse it into a date structure and then use simple comparison
| > operators.
|
|
| Not exactly as straight forward as you would think. Comparing a date with
| another for less than or equality, you need to not only check the date,
but
| you have to take into consideration the times.
|
| For example:
|
| Dim date1 As DateTime = DateTime.Parse("06/22/2005")
| Dim date2 As DateTime = Now()
|
| date1 is < date2...not equal to date2 :)
|
| Mythran
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top