Datetime difference in seconds

DateTime Difference:

Time difference refer to difference between two time stamp. some time we want to this for display time on the web page for example online Exam web site take this type of requirement or when user need to count login time and logout time and then calculate the time difference in which the visitor was open the web site.


Define Date time object in to asp.net

   DateTime now = DateTime.Now;  
DateTime is a class and now is the object of this class.

How to Add Minutes in datetime variable

For this we use fallowing method.
AddMinutes(<give the minutes here>);  

How to get timestamp:

TimeSpan ts = dateAfter5Minutes – now;
Here TimeSpan is type of class and ts is the object of this class.


Example of difference in seconds

In this Example we get the current time and add 5 munities in the current time and then display in an asp.net label control.

Code in asp.net :


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>



<!DOCTYPE html>

<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        
        DateTime now = DateTime.Now;

        Label1.Text = "now : " + now.ToString();

       
        DateTime dateAfter5Minutes = now.AddMinutes(5);

        TimeSpan ts = dateAfter5Minutes - now;
  
        int seconds = (int)ts.TotalSeconds;

        Label1.Text += "<br ><br />after ten minutes: ";
        Label1.Text += dateAfter5Minutes.ToString();

        Label1.Text += "<br ><br />difference after adding into datetime object : ";
        Label1.Text += seconds;
    } 
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title> difference in seconds</title>
</head>
<body>
    <form id="form2" runat="server">
    <div>
        <h2 style="color: MidnightBlue; font-style: italic;">
            datetime difference Example in asp.net Programming</h2>
        <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="get current datetime"
            OnClick="Button1_Click" Height="40" Font-Bold="true" />
    </div>
    </form>
</body>
</html>


Other Asp.net programming related Post:


Comments