Create A 3-Day Window Between 2 Calendars

J

JLuv

I have 2 calendar controls right now and i know how to compare the
dates from the 2 calendars using DateTime.Compare(). But that only
returns -1, 0, or 1. Is there a function that returns the difference in
the number of days?

What I want to do is allow only a 3 day gap between the 2 calendars.
 
K

Ken Cox [Microsoft MVP]

Hi,

You need to use a timespan for that. Here's some sample code.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Button1_Click _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim dtCal1 As DateTime
Dim dtCal2 As DateTime
Dim tmspan As TimeSpan
dtCal1 = Calendar1.SelectedDate
dtCal2 = Calendar2.SelectedDate
tmspan = dtCal1.Subtract(dtCal2)
Label1.Text = "The difference is " & _
tmspan.TotalDays.ToString & " day(s)."
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Days Difference</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:calendar id="Calendar1" runat="server"></asp:calendar>
<br />
<asp:calendar id="Calendar2" runat="server"></asp:calendar>
<br />
<asp:label id="Label1" runat="server"></asp:label>
<br />
<br />
<asp:button id="Button1" runat="server" onclick="Button1_Click"
text="Calculate" /></div>
</form>
</body>
</html>
 
J

JLuv

Sweet! Thanks!

This is a lot simpler than the function i was gonna create myself. lol

Hi,

You need to use a timespan for that. Here's some sample code.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Button1_Click _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim dtCal1 As DateTime
Dim dtCal2 As DateTime
Dim tmspan As TimeSpan
dtCal1 = Calendar1.SelectedDate
dtCal2 = Calendar2.SelectedDate
tmspan = dtCal1.Subtract(dtCal2)
Label1.Text = "The difference is " & _
tmspan.TotalDays.ToString & " day(s)."
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Days Difference</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:calendar id="Calendar1" runat="server"></asp:calendar>
<br />
<asp:calendar id="Calendar2" runat="server"></asp:calendar>
<br />
<asp:label id="Label1" runat="server"></asp:label>
<br />
<br />
<asp:button id="Button1" runat="server" onclick="Button1_Click"
text="Calculate" /></div>
</form>
</body>
</html>


JLuv said:
I have 2 calendar controls right now and i know how to compare the
dates from the 2 calendars using DateTime.Compare(). But that only
returns -1, 0, or 1. Is there a function that returns the difference in
the number of days?

What I want to do is allow only a 3 day gap between the 2 calendars.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top