Create a TimeSpan
CreateTimeSpan.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e) {
if(!this.IsPostBack)
{
Button1.Font.Bold = true;
Button1.ForeColor = System.Drawing.Color.Indigo;
Button1.Text = "Create 24 Hours TimeSpan And Add With Now";
Label1.Font.Size = FontUnit.Larger;
Label1.ForeColor = System.Drawing.Color.Crimson;
Label1.Font.Bold = true;
Label1.Font.Italic = true;
}
}
protected void Button1_Click(object sender, System.EventArgs e) {
DateTime now = DateTime.Now;
TimeSpan timeSpan24Hours = TimeSpan.FromHours(24);
DateTime modifiedDateTime = now + timeSpan24Hours;
Label1.Text = "Now: " + now.ToString();
Label1.Text += "<br />Modified DateTime[after added 24 hours TimeSpan]: " + modifiedDateTime.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net date time example: how to create a time span (TimeSpan) in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Teal">asp.net date time example: Create TimeSpan</h2>
<asp:Label
ID="Label1"
runat="server"
>
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
OnClick="Button1_Click"
/>
</div>
</form>
</body>
</html>

- How to add years to DateTime
- How to add milliseconds to DateTime
- How to get minutes between two DateTimes
- How to get days between two DateTimes
- How to subtract two DateTimes
- How to get day from a DateTime
- How to get the current time of day
- How to get the number of days in a given month
- How to get system DateTime
- How to add 1 month to a given DateTime object