Find datetime difference in asp.net by C#

How to find date difference in asp.net by C#:



In this Post we describe how to get the difference between two DateTime value in asp.net C#.In C# code by calling any of the overloads of the DateTime constructor that give permission  you to specify specific elements of the date and time value like year , month ,day,weekday and current time.



  • DateTime Structure: -This datatype  datatype is used to represent a time instant in C#.

  • TimeSpan Structure: -The TimeSpan datatype is used to represent time interval in asp.net C#.

Calculating DateTime Difference in C#:

Use TimeSpan data type of C# for calculating difference.
        TimeSpan ts = dateAfter2Minutes - now;

Calculating DateTime Difference in millisecond by C#:

int milliseconds = (int)ts.TotalMilliseconds;

Showing Difference between two datetime values in asp.net:

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>c# example - datetime difference in milliseconds</title>
    <style type="text/css">
        .style1
        {
            font-family: Arial, Helvetica, sans-serif;
            text-decoration: underline;
            color: #990000;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
        <h2 class="style1">
            Asp.net c# example - datetime difference
        </h2>
        <hr width="100%" align="left" color="Green" />
        <asp:Label ID="Label1" runat="server" Font-Size="Large" Font-Names="Comic Sans MS"
            Style="font-family: Arial, Helvetica, sans-serif; color: #0000FF">       
        </asp:Label>
        <hr width="100%" align="left" color="Green" />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Get Diffrence" OnClick="Button1_Click"
            Height="40px" Font-Bold="true" Width="112px" />
        <hr width="100%" align="left" color="Green" />
    </div>
    </form>
</body>
</html> 


C# code of Difference between two datetime values in asp.net:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class timediffrence : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //initialize a datetime variable with current datetime 
        DateTime now = DateTime.Now;

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

        //add 2 minutes to current time 
        DateTime dateAfter2Minutes = now.AddMinutes(2);

        TimeSpan ts = dateAfter2Minutes - now;
        //total milliseconds difference between two datetime object 
        int milliseconds = (int)ts.TotalMilliseconds;

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

        Label1.Text += "<br ><br />smillieconds difference between to datetime object : ";
        Label1.Text += milliseconds; 
    }
}

 
Find datetime difference in asp.net by C#
DateTime difference in asp.net by C#

Sql Server Interview Qus:

Related Post/Reference :


Asp.net Related Other Post:

Comments

Popular posts from this blog