I
I
itcry2019-05-04 19:37:59
Chromium
itcry, 2019-05-04 19:37:59

C# How to change date format in text string?

Hello! Running some tests on my site using Selenium + C# . I came across this unpleasant story:
The site has a block in which 3-5 lines of text in this format:
Hard disk usage periods:
from 11/01/2011 to 02/21/2012: Datacenter1
from 03/28/2012 to 08/12/2017: Datacenter2
from 08/12/2017 .2017 and at the moment: Datacenter3
I get these lines in the collection in this way:

IList<IWebElement> Collection = Driver.FindElements(By.CssSelector("#checkHardDrive > div > ul.Periods li"));

After that, in my program I display information in WindowsForm in a text box in which multiline is enabled:
foreach (IWebElement element in Collection )
            {
                this.period.BeginInvoke(updateTextPeriod, element.Text);
                Thread.Sleep(100);

            }

In principle, everything works, but there is one BUT.
The date in the textbox is displayed as 11/1/2011 to 2/21/2012, etc.
Those. you need to change the format of the date that is displayed in the text box.
Tell me if I understand correctly that I need to take each element, parse it using regular expressions to numbers, write the values ​​\u200b\u200bin 1 variable, then parse the date itself, then again the text, then again the date, etc., then separately translate the format dates and after that glue everything back into one line and display it in the text box?
Just suddenly there is some solution simpler?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#, 2019-05-05
@mindtester

1 - if the date is displayed in your browser as you need * - look for another way to get the data
2 - if this is not possible, you will have to parse each line yourself, select the dates, convert them, and only then display them. such is the programmer's
life

after that, glue everything back into one line
it's not necessary, it's enough to replace the date in the "bad format" with the "good" one
pps * I guess you are using Selenium? if yes and
in the browser the date is displayed as you need
- 99% that there is a way to get the string you need right away. if not, in general, such a task is not very difficult, and quite useful for developing skills

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question