How to find minute difference between two times

May 23, 2014 0 Comments

If you want to get minute difference between two times then you should use TimeSpan Structure for that. First to create two DateTime object. Now, create difference between two dates using TimeSpan structure also get minutes using TotalMinutes property of TimeSpan structure.

Source Code

<form id="form1"
     runat="server">
    <asp:Button ID="Button1"
    runat="server"
    BackColor="#FF33CC"
    onclick="Button1_Click"
    Text="Click"
     Width="73px" />
    <div>  
    <asp:Label ID="Label1"
     runat="server"
     BackColor="Yellow"
     Text="Label"></asp:Label>  
    </div>
    </form>

Code Behind

protected void Button1_Click(object sender, EventArgs e)
    {
        {            
            DateTime now = DateTime.Now;
            Label1.Text = "now : " + now.ToString();
           DateTime dateAfter4Hours = now.AddHours(4);
            TimeSpan TS = dateAfter4Hours - now;            
            int minutes = (int)TS.TotalMinutes;
            Label1.Text += "<br ><br />after four hours: ";
            Label1.Text += dateAfter4Hours.ToString();
            Label1.Text += "<br ><br />This is difference between to datetime object in minutes  : ";
            Label1.Text += minutes;
        }

    }
Code Generate the following output
How to find minute difference between two times

Jackrin Reacher

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google

0 comments: