How to select current Date in Selenium c# - c#

I have calendar that need to select current date I have code but every time new month start then my code selects next day not current day. Thing is on my UI default day is next day shown.
IList<IWebElement> datePicker = driver.FindElements(By.Xpath("element"));
foreach(IWebElement td in datePicker)
{
string date = td.Text;
DateTime currentDate = DateTime.Now;
var todayDate = currentDate.Date.ToString("d");
if(date.Equals(todayDate))
{
td.Click;
break;
}
}

Related

How to select date range from a asp.net calendar control in a single calendar control

i am having a doubt that how can i select date range in asp.net calendar control
for example i will give start date and end date so that all the dates in between those dates should get selected.
Can any one help me in this please
You need to use the SelectedDatesCollection
This is an example from the link...
// Iterate through the current month and add all Wednesdays to the
// SelectedDates collection of the Calendar control.
for (int i = 1; i <= System.DateTime.DaysInMonth(current_year, current_month); i++)
{
DateTime currentDate = new DateTime(current_year, current_month, i);
if (currentDate.DayOfWeek == DayOfWeek.Wednesday)
{
DisplayCalendar.SelectedDates.Add(currentDate);
}
}

How to get 3 days back of current date and should not be weekends

How can I get 3 days back of current date and should not fall on weekends (Sat/Sun)
Let's say if date i select date as 03/28/2017. It should display date as 03/23/2017. It should not take sat/sun.
dto.ProcessEndDate.AddDays(-3);
This code assumes that if new date falls on a weekend, it will instead return the Friday before.
public static DateTime GetDateExcludeWeekends(DateTime date, int index)
{
var newDate = date.AddDays(-index);
if(newDate.DayOfWeek == DayOfWeek.Sunday)
{
return newDate.AddDays(-2);
}
if(newDate.DayOfWeek == DayOfWeek.Saturday)
{
return newDate.AddDays(-1);
}
return DateTime.Now;
}
You can tweak the logic, but the main thing is to look at the DayOfWeek enum property of the DateTime class.

Calculate Last Day of the Next Month

I am attempting to use the DateTime function in C# to calculate the last day of next month.
For example, today is December 17th 2015. I want the DateTime function to return January 31st 2016 (the last day of next month).
I am using the following to calculate the first day of next month (this works):
DateTime firstDayNextMonth = DateTime.Today.AddDays(-DateTime.Now.Day+1).AddMonths(1);
DateTime reference = DateTime.Now;
DateTime firstDayThisMonth = new DateTime(reference.Year, reference.Month, 1);
DateTime firstDayPlusTwoMonths = firstDayThisMonth.AddMonths(2);
DateTime lastDayNextMonth = firstDayPlusTwoMonths.AddDays(-1);
DateTime endOfLastDayNextMonth = firstDayPlusTwoMonths.AddTicks(-1);
Demo: http://rextester.com/AKDI52378
//system date or any date u want this case it is a calendar picker - 22/03/2016
DateTime today = dtpFrom.Value;
//Add a month to your date example , it now becomes - 22/04/2016
DateTime endOfMonth = new DateTime(today.Year, today.Month,today.Day).AddMonths(1);
//Get the last date off the above which is - 30
int getlastday = DateTime.DaysInMonth(endOfMonth.Year, endOfMonth.Month);
//Now set the date to the value which will be the last day off the next month - 30/04/2016
DateTime newDate = new DateTime(endOfMonth.Year, endOfMonth.Month, getlastday);
DateTime.DaysInMonth(DateTime.Now.AddMonths(1).Year, DateTime.Now.AddMonths(1).Month);
var lastDayInNextMonth = DateTime.DaysInMonth(DateTime.Now.AddMonths(1).Year, DateTime.Now.AddMonths(1).Month );
# Ben : DateTime.Now.AddMonths(1) will add 1 month to the current date not substract 11 months.
DateTime.Now.AddMonths(1).Year will give 2016 not 2015 refer the attached image
try this:
int Day= DateTime.DaysInMonth(DateTime.Now.Year, DateTime.Now.Month+1>12 ? 01 : DateTime.Now.Month+1 );

Days remaining from start date is showing incorrectly

I have a repeater that displays an item that a user wants to sell. In one columns of the repeater, the CreateDate or the date the item was posted is displayed like so:
//lblExp.Text originally contains 2013-05-24 14:24:08.000
Label lblExp = (Label)e.Item.FindControl("lblExp");
DateTime StartDate = Convert.ToDateTime(lblExp.Text);
DateTime expDate = StartDate.AddDays(30);
int DaysLeft = (expDate - StartDate).Days;
lblExp.Text = DaysLeft.ToString();
When that code executes I get 30 instead of 28.
Is this what you're trying to say? (replacing StartDate with DateTime.Now)
//lblExp.Text originally contains 2013-05-24 14:24:08.000
Label lblExp = (Label)e.Item.FindControl("lblExp");
DateTime StartDate = Convert.ToDateTime(lblExp.Text);
DateTime expDate = StartDate.AddDays(30);
int DaysLeft = (expDate - DateTime.Now).Days; // Replaced here
lblExp.Text = DaysLeft.ToString();

Setting A particular Day in a date

I am using Calender Extender Control in the AjaxControlToolkit. There are basically 2 controls of date : Start Date and End date (both associated with calender extender). Based on start Date selected, I populate date in the end date field like adding no of months or days. But like I have been able to add months, but also wants to set a particular day of that month which I am unable to do.
Example:
Today date is 18 Dec 2012. Something like 1st of every three months, So I add 3 months the month comes out to be Feb 2013. But I want to set Day 1st Feb 2013. I am unable to do it. Kindly help.
You can set whatever day of month by add month.
DateTime todayDate = DateTime.Now;
DateTime after3MonthDate = todayDate.AddMonths(3);
//Set First Day of Month
after3MonthDate = new DateTime(after3MonthDate.Year, after3MonthDate.Month, 1);
This code can be used for existing date time variable to set the day part to the first day of the month:
if(myDate.Day > 1)
{
myDate = myDate.AddDays(-(myDate.Day - 1));
}
Try this:
// Here is the simple wrapper method to get the first day of the month:
public DateTime FirstDayOfMonthFromDateTime(DateTime dateTime)
{
return new DateTime(dateTime.Year, dateTime.Month, 1);
}
// Set the due date...
DueDate.Text = (FirstDayOfMonthFromDateTime(DateTime.Parse(StartDate.Text).AddMonths(N))).ToShortDateString();
You can also modify the wrapper method to get any day of the month:
public DateTime DayOfMonthFromDateTime(DateTime dateTime, int day)
{
return new DateTime(dateTime.Year, dateTime.Month, day);
}

Categories

Resources