Validate Date format - comes with error

Ask general questions here.
Lyuba Boerma
Posts: 27
Joined: Mon Apr 01, 2019 12:12 pm

Validate Date format - comes with error

Post by Lyuba Boerma » Fri May 17, 2019 9:48 am

Hi everybody)
i have a question.
i need to validate , that date and time in a application is in correct format .

i have this code for this

Code: Select all

dateFormatString = "dd-MM-yyyy HH:mm";
            	
            	try
            	{
            		System.DateTime dt = System.DateTime.ParseExact(dateString, dateFormatString, System.Globalization.CultureInfo.InvariantCulture);
            		Report.Log(ReportLevel.Info, "Validated date is  correct   " + dateString + "\r\nValidated date format: " + dateFormatString);
            	}
            	catch
            	{
            		Report.Log(ReportLevel.Warn, "Validated date in incorrect format. \r\nValidated date: " + dateString + "\r\nValidated date format: " + dateFormatString);
            	}
i validate time in two places.
i one it passes validation, in second - it gives me an error... And i don't understand why..
Same string validated to same value gives me different results...
Maybe you can help me to understand why? i even check a number of "spaces"..
what can be an issue?

this comes correct
Validated date is correct 17-05-2019 10:19
Validated date format: dd-MM-yyyy HH:mm

this comes with error
Validated date in incorrect format.
Validated date: 17-05-2019 10:19
Validated date format: dd-MM-yyyy HH:mm

report.png
report.png (38.93 KiB) Viewed 80 times

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 5071
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Validate Date format - comes with error

Post by odklizec » Fri May 17, 2019 9:54 am

Hi,

There appears to be an empty space behind the date string?...
datetime.png
datetime.png (90.82 KiB) Viewed 78 times
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

Lyuba Boerma
Posts: 27
Joined: Mon Apr 01, 2019 12:12 pm

Re: Validate Date format - comes with error

Post by Lyuba Boerma » Fri May 17, 2019 10:00 am

Thanks! maybe it is an reason

Is it possible to parse this value without extra spaces???
or maybe to make a limit for date string to 16 symbols?)
Thanks ))

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 5071
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: Validate Date format - comes with error

Post by odklizec » Fri May 17, 2019 10:35 am

Hi,
Trim() should do the trick...

Code: Select all

dateString = dateString.Trim();
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

Lyuba Boerma
Posts: 27
Joined: Mon Apr 01, 2019 12:12 pm

Re: Validate Date format - comes with error

Post by Lyuba Boerma » Fri May 17, 2019 3:15 pm

Thank you
It really helps!