-->

Friday, May 23, 2014

How to find minute difference between two times

How to find minute difference between two times

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

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved