How to validate if string is a date in specific format?
How to validate if string is a date in specific format?
I have list of dates formats. I want to verify that in my app, specific string is changed according the format selected in settings.
Re: How to validate if string is a date in specific format?
Hi,
I would try something like this:Simply, pass the date and expected format to above method and if date is incorrect in format, ParseExact fails and so it logs failure in Ranorex report. Hope this helps?
I would try something like this:
Code: Select all
using System.Globalization;
/// <summary>
/// validate date format
/// </summary>
/// <param name="dateString">date string</param>
/// <param name="dateFormatString">expected date format</param>
public void ValidateDateFormat(string dateString, string dateFormatString)
{
try
{
System.DateTime dt = System.DateTime.ParseExact(dateString, dateFormatString, System.Globalization.CultureInfo.InvariantCulture);
}
catch
{
Report.Log(ReportLevel.Failure, "Validated date in incorrect format. \r\nValidated date: " + dateString + "\r\nValidated date format: " + dateFormatString);
}
}
Pavel Kudrys
Ranorex explorer at Descartes Systems
Please add these details to your questions:
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