programming we consider all important topic for knowledge about controls,web Services, JSON, Ajax, database, and interview related Question. In this and sql server tutorial blog we give many examples of jquery as like How Play YouTube Video in Your Website by URL, How to Play YouTube Videos in by using Jquery , Jquery Change div background on mouseover in , JQuery UI Datepicker (Calendar) etc.

Saturday, October 11, 2014

Find datetime difference in by C#

How to find date difference in by C#:

In this Post we describe how to get the difference between two DateTime value in 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 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

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head id="Head1" runat="server">
    <title>c# example - datetime difference in milliseconds</title>
    <style type="text/css">
            font-family: Arial, Helvetica, sans-serif;
            text-decoration: underline;
            color: #990000;
    <form id="form1" runat="server">
    <div align="center">
        <h2 class="style1">
   c# example - datetime difference
        <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">       
        <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" />

C# code of Difference between two datetime values in

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 by C#
DateTime difference in by C#

Sql Server Interview Qus:

Related Post/Reference : Related Other Post:

No comments:

Post a Comment