How to compare two date (month and year)

Hi,
after a scraping, I collect dates from a web page, conformed: “MM.dd.yy”. I have to compare this date if it is less than or equal to today, but the verification must be done only on month and year.
Then if MM/yy (of scrap) < today (MM/yy)

how should the string be inserted? The collected date is in DateTime format.

Thanks

1 Like

Hi @AaronMark

You should convert it to date and compare date by a month or year by

VariableDate.month < now.month
or
VariableDate.year < now.year

Check date is less than or equal to today condiion

(checkdate.Year < now.Year or (checkdate.Year = now.Year And checkdate.Month < now.Month)) or (checkdate.Year = now.Year And checkdate.Month = now.Month)

3 Likes

Hi…
@AaronMark convert your string variable of date to Date format
.
not CDate(stringDateVariable).ToString(“MM/yyyy”) > Now.ToString(“MM/yyyy”)

1 Like

Refine version of your solution:

If (dDate.Year < DateTime.Now.Year Or (dDate.Year = DateTime.Now.Year And dDate.Month <= DateTime.Now.Month)) Then
  'something                
 Else
  'something
End If

Source: vb.net - How to compare only month and year? [VB] - Stack Overflow
Note: If wants to compare with other than today’s date then just replace DateTime.Now with your date variable!

Hello Aaron,
In this video I do a lot of stuff with String and DateTime:

0:35 Examples for Substring functions
4:10 IndexOf and LastIndexOf
5:00 SubString working together IndexOf and LastIndexOf
6:45 Split string by character
8:50 Split string by string
12:00 Lower Case and Upper Case
12:45 Trim
15:05 Compare strings in multiple ways
19:05 Resume of all the String function part
20:05 DateTime to string in multiple ways
23:00 AddDays get DayofWeek
25:40 Convert from String to DateTime
26:20 Compare DateTime

Thanks,
Cristian Negulescu