c# - How to add a TimeSpan to a DateTime object

DateTime add TimeSpan
datetime-add-timespan.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
    
<!DOCTYPE html>      
<script runat="server">    
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        //initialize a datetime variable with current time
        DateTime now = DateTime.Now;

        //create a timespan with 2 hours 30 minutes and 50 seconds
        TimeSpan ts = new TimeSpan(2,30,50);

        //this line added a timespan with current time
        DateTime afterAddedTimeSpan = now.Add(ts);

        Label1.Text = "current time : " + now.ToLongTimeString();

        Label1.Text += "<br /><br />";
        Label1.Text += "after added a timespan with current time.........<br />";
        Label1.Text += "time span: 2 hours|30 minutes|50 seconds.........<br />";
        Label1.Text += afterAddedTimeSpan.ToLongTimeString();
    }
</script>      
      
<html xmlns="http://www.w3.org/1999/xhtml">      
<head id="Head1" runat="server">      
    <title>c# example - datetime add timespan</title>      
</head>      
<body>      
    <form id="form1" runat="server">      
    <div>      
        <h2 style="color:MidnightBlue; font-style:italic;">      
            c# example - datetime add timespan
        </h2>      
        <hr width="550" align="left" color="Gainsboro" />      
        <asp:Label       
            ID="Label1"       
            runat="server"      
            Font-Size="X-Large"    
            Font-Names="Comic Sans MS"
            >      
        </asp:Label>      
        <br /><br />    
        <asp:Button       
            ID="Button1"       
            runat="server"       
            Text="add timespan to current time"      
            OnClick="Button1_Click"    
            Height="40"      
            Font-Bold="true"      
            />      
    </div>      
    </form>      
</body>      
</html>
More c# examples