asp.net c# tutorials and examples code for web developers

Wednesday, January 14, 2009

How to get total milliseconds difference between two date time object in asp.net

asp.net date time example: how to get total milliseconds difference between two date time object
TotalMilliseconds.aspx
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e) {
        DateTime myDateTime1 = DateTime.Now;
        DateTime myDateTime2 = DateTime.Now.AddSeconds(5);
        TimeSpan difference = myDateTime2.Subtract(myDateTime1);

        double totalMilliseconds = difference.TotalMilliseconds;

        Label1.Text = "MyDate1: " + myDateTime1.ToString();
        Label1.Text += "<br />MyDate2: " + myDateTime2.ToString();
        Label1.Text += "<br /><br />Total Milliseconds Difference: " + totalMilliseconds;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>asp.net date time example: how to get total milliseconds difference between two date time object</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Red">asp.net date time example: Milliseconds difference</h2>
        <asp:Label 
             ID="Label1" 
             runat="server" 
             Font-Size="Larger"
             Font-Italic="true"
             Font-Bold="true"
             ForeColor="DeepSkyBlue"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             Font-Bold="true" 
             ForeColor="DeepSkyBlue" 
             OnClick="Button1_Click"
             Text="Get Toatal Milliseconds Difference"
             />   
    </div>
    </form>
</body>
</html>






Related asp.net examples