Not able to convert my date variable

I have a project where I have to verify that extracted date from the web page. Extracted date is stored in data table. I am using the below condition and when I run this, I am getting error as String was not recognized as a valid DateTime and the condition is shown below:

Convert.ToDateTime(AssigneeDocument.Item(“DatefromWeb”).ToString) > DateTime.Today.AddDays(-90)

Kindly let me know what is wrong here

@subashini Is the date extracted from the web page always the same format?

@subashini

Try the converstion as below

DateTime.ParseExact(AssigneeDocument.Item("DatefromWeb").ToString,"MM/dd/yyyy",System.Globalization.CultureInfo.InvariantCulture) > DateTime.Today.AddDays(-90)

Here MM/dd/yyyy is the format of input date string…depending on your string change the format…for hours and minute sand seconds hh:mm:ss : or / is general separation …that has nothing to do with the format

hope this helps

Cheers

Hi @subashini

Give a try to this.
DateTime.ParseExact(AssigneeDocument.Item(“DatefromWeb”).ToString,{“M/d/yyyy”,“dd/MM/yyyy”,“d/M/yyyy”,“MM/dd/yyyy”,“MM/dd/yyyy hh:mm:ss”},System.Globalization.CultureInfo.InvariantCulture) > DateTime.Today.AddDays(-90)

Here {“M/d/yyyy”,“dd/MM/yyyy”,“d/M/yyyy”,“MM/dd/yyyy”,“MM/dd/yyyy hh:mm:ss”} is the array of possible input date format. If your input date fromat other than above mentioned then add that format to this list.

Hope this helps.

HI,

First of all, can you check (and share) content of AssigneeDocument.Item("DatefromWeb").ToString using WriteLine activity etc?

If it’s illegal date time style including empty string, it’s necessary to check whether valid date string in advance.
If there is extra white space, Trim method may work.
If it’s dd/MM/yyyy style, it’s necessary to use DateTime.ParseExact with format string"dd/MM/yyyy" for example.

Regards,

yes always same format. for eg 22.12.2022

yes, I added a write line activity to see what is the output for '(AssigneeDocument.Item(“DatefromWeb”) and the date is 22.12.2022

Hi @subashini

Give a try to this.

DateTime.ParseExact(AssigneeDocument.Item("DatefromWeb").ToString,"dd.MM.yyyy",System.Globalization.CultureInfo.InvariantCulture) > DateTime.Today.AddDays(-90)

image

Hope this helps

I tried this and still got the same error

@subashini

Please use a log message before this and check the date format youa re getting or show it so that we can give you a correct format…

And also check if you are getting blank values sometimes if blank values are coming then we can detect them before hand and then use a condition so that the bot would not try to convert a blank value to date and fail

Hope this helps

cheers

Hi @subashini

Please check this

Sorry! This worked like a charm. Overlooked the comment.

1 Like

@subashini

Happy Automation

Please mark solution for appropriate comment and close if resolved…so that others also can get help

Cheers

Thanks a lot. This worked like a charm.

1 Like