Saturday, 23 May 2015

Handling dates and time manipulation in Selenium and C#

Date fields are very common in web applications. So you should know how to perform below operations on date in C#.Net in case you are using C# + Selenium API.

  1. Find current system date/time and format it as required.
  2. Find next nth business date (Excluding holidays and weekends)
  3. Find previous nth business date (Excluding holidays and weekends)
  4. Find the difference between 2 dates in terms of days, months, weeks, years.
  5. Compare 2 dates.
  6. Convert string to date and vice versa

Let us try to go one step at a time to learn how we can do all these operations.
Finding current system date in C#.Net

DateTime now = DateTime.Now;
             String formattedDate1 = now.ToString("MMM ddd d HH:mm yyyy");
             String formattedDate2 = now.ToString("MM/dd/yyyy");
             String formattedDate3 = now.ToString("dd/MM/yyyy");
             String formattedDate4 = now.ToString("dd-MMM-yyyy");

             IFormatProvider culture = new System.Globalization.CultureInfo("en-AU", true);
             DateTime newDate = DateTime.Parse(formattedDate3, culture, System.Globalization.DateTimeStyles.AssumeLocal);
             Console.WriteLine("Year: {0}, Month: {1}, Day {2}", newDate.Year, newDate.Month, newDate.Day);


What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at

No comments:

Post a Comment

Buy Best Selenium Books