Error : String was not recognized as a valid DateTime

Hello,

I’m reading an outlook email with the current date.

Here is the code:

EmailMessage.Where(Function(x) DateTime.ParseExact(x.Headers(“Date”), “MM/dd/yyyy HH:mm:ss”, Nothing).Date.Equals(DateTime.Now.Date))

I’m getting a “String was not recognized as a valid DateTime.” error

Here is the system Date & Time setting

e1

Best Regards,
Vrushali

Please check the below

OutMails(0).Headers(“Date”) is string type and where Date.now.Date is Datetime type.

image

So to convert your header to datetime variable try using Cdate as shown below.

image

Hope this helps…

If this does not help…Try printing your header date format and check the format and then based on that tweak your code…

@prasath17

Could you please provide the solution with the following code?

EmailMessage.Where(Function(x) DateTime.ParseExact(x.Headers(“Date”), “MM/dd/yyyy HH:mm:ss”, Nothing).Date.Equals(DateTime.Now.Date))

I just added .tolist() at the end …and your code works good …

Count 4 is correct, i have verified in my outlook.

1 Like

@prasath17 Thank you for your response. But it’s not worked for me.

This error occurring while running the code on VM.

The same code does not throw any error on laptop.

Email Date format : Tue 15 Jun 2021 00:00:00 +0530

System Date : 06/15/2021

As per the code :

EmailMessage.Where(Function(x) DateTime.ParseExact(x.Headers(“Date”), “MM/dd/yyyy HH:mm:ss”, Nothing).Date.Equals(DateTime.Now.Date))

The bot converting email date as system date then comparing.

I’m not getting why the error occurring

Best Regards,
Vrushali

You mean this is the date format when you print EmailMessage(0).Headers(“Date”)??

@Vrushali_Gave

Check for conversion of DateTime Format for your reference

Hope this may help you

Thanks

Did you try with System.Globalization.CultureInfo.InvariantCulture ?

As for your code:

EmailMessage.Where(Function(x) DateTime.ParseExact(x.Headers(“Date”), “MM/dd/yyyy HH:mm:ss”, System.Globalization.CultureInfo.InvariantCulture).Date.Equals(DateTime.Now.Date))

If you just use Datetime.Parse() instead of ParseExact() then you don’t have to worry about all that extra stuff. It figures out the format for you and gives back what you want.

1 Like

I am afraid it won’t. It works similar to cdate where it takes the local settings of your system.

please check the below statements, since mine is MM/dd/yyyy datetime.parse accepts that format not dd/MM/yyyy format.

So you have to use correct IFormatProvider to recognize your date…

2 Likes

Great info, thanks!

@prasath17 Yes

@prasath17 @Srini84 @kumar.varun2 @postwick

Unable to solve the issue.
PFA screenshot

Your path isn’t a proper path. You have a / instead of a \

@postwick Yes right. I’ll correct it. But the error is different.

Is there any solution to come out from the issue?

The two systems probably have different system date format settings.

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