Reputation: 571
im using vs 2010.
the below code is how i've put 2 dates in the 2 text boxes.I want to know how i can calculate the no. of days between them.If possible can u tell me how to also take off Sundays from that number.thnx in advance
protected void LinkButton1_Click(object sender, EventArgs e)
{
Calendar1.Visible = true;
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
TextBox3.Text = Calendar1.SelectedDate.ToLongDateString();
Calendar1.Visible = false;
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
Calendar2.Visible = true;
}
protected void Calendar2_SelectionChanged(object sender, EventArgs e)
{
TextBox4.Text = Calendar2.SelectedDate.ToLongDateString();
Calendar2.Visible = false;
}
Upvotes: 2
Views: 5700
Reputation: 2123
You can use DateTime.Substract to do the calculation.
Calculating the sundays and removing them should be easy, using the returned TimeSpan object.
EDIT
Jon is correct with his comment, and its not trivial to get the sundays using just the TimeSpan. The link provided in the comment bellow (by louis) seems to do this part of the job though
Upvotes: 1
Reputation: 2548
Calculate a TimeSpan
by Subtracting two DateTime
values.
The following code should get you started.
if ( Calendar2.SelectedDate != null && Calendar1.SelectDate != null )
{
TimeSpan Value;
if ( Calendar2.SelectedDate >= Calendar1.SelectedDate )
Value = (Calendar2.SelectedDate - Calendar1.SelectedDate).TotalDays;
else
Value = (Calendar1.SelectedDate - Calendar2.SelectedDate).TotalDays;
}
I currently do not have access to Visual Studio, I believe this to compile, its just a quick and dirty suggestion.
public Nullable SelectedDate { get; set; }
Upvotes: 0
Reputation: 191037
This should work.
var days = (Calendar1.SelectedDate - Calendar2.SelectedDate).TotalDays;
However, this does not exclude Sundays.
Upvotes: 3