Subtracting dates giving me wrong value

Hi all,

Hopefully the last issue for this bot:

I have scraped the date from a table and am subtracting today’s date from it to give me a value.

The date being scraped is correct and so is today’s date, but it is giving me the wrong delta. It worked before and I wonder if this is because of the date format (ie 2 digits in day column now opposed to one). I need a formula that will scrape and calculate the change in dates regardless if there are 1 or 2 months in the month and day digits. Does anyone know how to do this? I will post my current calcs below:

Extract last row in table: CellCount = ExtractDataDate.Rows.Count-1
Get Date: MySingleCell = ExtractDataDate.Rows(cellCount).Item(0).ToString
*then i split and trimmed the string to get what i wanted and made variable ‘yourdateasstring’
converted string 2 date:
yourdate = DateTime.ParseExact(YourDateAsString,“M/d/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

Took today’s date: todaysdateandtime = datetime.Now

took out time: todaysdateraw = todaysdateandtime.Split(" "c)(0).ToString

Converted today’s date:
todaysDate = DateTime.ParseExact(todaysdateraw,“M/d/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

Subtracted dates:
daydelta = yourDate-todaysDate

Hello @Double-D,
Try this.
(DateTime.Parse(yourDate, System.Globalization.CultureInfo.InvariantCulture).Date - DateTime.Parse(DateTime.Today.ToString, System.Globalization.CultureInfo.InvariantCulture)).TotalDays

@karavinds I think that worked perfectly (but not positive what you did and what I did wrong haha). Thank you!

@karavinds

Hey, so I am now getting an error because the date that I am taking from the website (yourdate variable above) is converted to date with yourdate = DateTime.ParseExact(YourDateAsString,“M/dd/yyyy”,System.Globalization.CultureInfo.InvariantCulture)

It is giving me an error because the date is now 8/1/2019 so the format is wrong. How can I make it so it will convert it to a date to allow the formula you provided to work but to work with any month and day format (example: dd/mm/yyyy, d/mm/yyyy, dd/m/yyyy, d/m/yyyy) ?

I think I fixed it by doing just converting string into a date without a specified format and then using your formula.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.