We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Comparing string to datetime.

mathieu_cupryk
on
Medium Priority
272 Views
Last Modified: 2012-05-06
I have the following:

Dim value As String = String.Empty
 value = filename.Substring(4, 11)
                                                 
value = 20090210
I need to compare this to datetime and return true if older than 7 days.
Comment
Watch Question

Fernando SotoRetired
CERTIFIED EXPERT
Distinguished Expert 2017

Commented:
Hi mathieu_cupryk;

This should do what you need.

value = "20090210"

Dim dt As DateTime = New DateTime(Int32.Parse(value.Substring(0, 4)), _
                                  Int32.Parse(value.Substring(4, 2)), _
                                  Int32.Parse(value.Substring(6, 2)))

Dim ts As TimeSpan = dt - Today

If ts.Days > 7 Then
    ' Delete your files in here
End If


Fernando
Éric MoreauSenior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
dim dt as new date(value.substring(0,4), value.substring(4,2),value.substring(6,2))
if dt < date.today.adddays(-7) then
    'older then 7 days
endif
Retired
CERTIFIED EXPERT
Distinguished Expert 2017
Commented:
Sorry; this line of code

Dim ts As TimeSpan = dt - Today

Should be this:

Dim ts As TimeSpan = Today - dt

Fernando

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.