Get difference in datetime variables

I have a date time variable which is scraped from the web (lets call it scrapedTime). Now I want to compare this variable to DateTime.Now and see how many days apart they are.

Something like:
DateTime.Now - scrapedTime = 5 days
I tried doing it like this^ already but I get an error: “Value of type system.timespan cannot be converted to date”. The thing is I am not even using a system.timespan variable

could someone please let me know how I can do this.

First of all, assign the scraped text to a perfect date variable using the condition and then check for the difference @Sami_Syed

DateTime.ParseExact(“textYouHave”, “mm-dd-yyyy”, System.Globalization.DateTimeFormatInfo.InvariantInfo).ToString(“dd-mm-yyyy”)

I am getting the error string was not recognized as a valid date time.

Let me go over what I have so far.

  1. Scrape text from web. Note - It’s format is like so “September 25 at 9:18 PM”. All I care about is date, not time. I saved this string as timeSTR

  2. Convert to date time variable, so for that I did used code:
    scrapedDate = Datetime.ParseExact(Split(timeSTR.tostring," “)(0).tostring+” “+Split(timeSTR.tostring,” “)(1).tostring+” "+now.tostring(“yyyy”),“MMMM dd yyyy”,system.globalization.cultureinfo.invariantculture)

Now when I try to add your code I did it like so (and added it as step 3):
resultSTR = DateTime.ParseExact(timeSTR, “mm-dd-yyyy”, System.Globalization.DateTimeFormatInfo.InvariantInfo).ToString(“dd-mm-yyyy”)

Let me know

The format should be the format of the text you are scraping from the web @Sami_Syed

I hope that is what you are missing

1 Like