Determine DateTime before another date
datetime-before.aspx
<%@ Page Language="C#" AutoEventWireup="true"%>
<!DOCTYPE html>
<script runat="server">
public static bool Before(DateTime dateToCheck, DateTime dateToCompare)
{
return (dateToCheck < dateToCompare);
}
protected void Button1_Click(object sender, System.EventArgs e)
{
//initialize a datetime variable with today
DateTime today = DateTime.Today;
//create a timespan object
TimeSpan ts = new TimeSpan(1, 0, 0, 0);
DateTime yesterday = today.Subtract(ts);
DateTime nextWeek = today.AddDays(7);
Label1.Text = "Today : " + today.ToLongDateString();
Label1.Text += "<br />Yesterday : " + yesterday.ToLongDateString();
Label1.Text += "<br />Next Week : " + nextWeek.ToLongDateString();
Label1.Text += "<br /><br />";
Label1.Text += "Is yesterday before today? ";
Label1.Text += Before(yesterday, today);
Label1.Text += "<br /><br />";
Label1.Text += "Is nextweek before today? ";
Label1.Text += Before(nextWeek, today);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>c# example - datetime before</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
c# example - datetime before
</h2>
<hr width="550" align="left" color="Gainsboro" />
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
Font-Names="Comic Sans MS"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="check datetime before another date"
OnClick="Button1_Click"
Height="40"
Font-Bold="true"
/>
</div>
</form>
</body>
</html>

- How to get the number of days in a given month
- How to create a TimeSpan
- How to add weeks to a DateTime object
- How to add a TimeSpan to a DateTime object
- How to get the first day of a month using a given date
- How to get the last day of month using a given date
- How to get the month name from month number
- How to get the first day of week
- How to get the integer value of day of week
- How to get two DateTime objects difference in milliseconds