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;
             Console.WriteLine(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");
             Console.WriteLine(formattedDate1);
             Console.WriteLine(formattedDate2);
             Console.WriteLine(formattedDate3);
             Console.WriteLine(formattedDate4);


             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);

             Console.WriteLine(newDate.AddDays(3));


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

No comments:

Post a Comment

Buy Best Selenium Books