Home > Blockchain >  How to parse text with UTC from IWebElement into date. Selenium and C#
How to parse text with UTC from IWebElement into date. Selenium and C#

Time:02-19

I've got one problem. I can't parse text from IWebElement into dateTime with UTC.

Selenium without any problem takes text from page:

14.02.2022 09:46:12 UTC

And path to element is correct :

 string dateOfAction = Driver.FindElement(By.XPath($"//div[1]//span[1]")).Text;

I would like to parse this string from dateOfAction(with this same UTC format) into date.

 var actionDate = DateTime.ParseExact(dateOfAction, "dd/MM/yyyy HH:mm:ssZ", null).ToUniversalTime();

if try get string dateOfActionI have "14/02/2022 09:46:12 UTC". But if I trying do

var actionDate = DateTime.ParseExact(dateOfAction, "dd/MM/yyyy HH:mm:ssZ", null).ToUniversalTime(); or just try var actionDate = DateTime.Parse(dateOfAction); the result is not as I expect {1/1/0001 12:00:00 AM}

How to correctly parse string dateOfAction to DateTime dd/MM/yyyy HH:mm:ss with UTC like in span above?

CodePudding user response:

I see you direct issue is solved but please cf. to this answer for more information.

CodePudding user response:

Thanks for answers!

I've done

var actionDate = DateTime.ParseExact(dateOfAction, "dd/MM/yyyy HH:mm:ss UTC", null).ToUniversalTime();

and it solved my problem

  • Related