Thursday, 27 March 2014

Date Time Difference in Millisecond Using C#

  1. <%@ Page Language="C#" AutoEventWireup="true"%>  
  2.       
  3. <!DOCTYPE html>        
  4. <script runat="server">  
  5.     protected void Button1_Click(object sender, System.EventArgs e)  
  6.     {  
  7.         //initialize a datetime variable with current datetime  
  8.         DateTime now = DateTime.Now;  
  9.   
  10.         Label1.Text = "now : " + now.ToString();  
  11.   
  12.         //add 2 minutes to current time  
  13.         DateTime dateAfter2Minutes = now.AddMinutes(2);  
  14.   
  15.         TimeSpan ts = dateAfter2Minutes - now;  
  16.         //total milliseconds difference between two datetime object  
  17.         int milliseconds = (int)ts.TotalMilliseconds;  
  18.          
  19.         Label1.Text += "<br ><br />after two minutes: ";  
  20.         Label1.Text += dateAfter2Minutes.ToString();  
  21.   
  22.         Label1.Text += "<br ><br />smillieconds difference between to datetime object : ";  
  23.         Label1.Text += milliseconds;  
  24.     }  
  25. </script>        
  26.         
  27. <html xmlns="http://www.w3.org/1999/xhtml">        
  28. <head id="Head1" runat="server">        
  29.     <title>c# example - datetime difference in milliseconds</title>        
  30. </head>        
  31. <body>        
  32.     <form id="form1" runat="server">        
  33.     <div>        
  34.         <h2 style="color:MidnightBlue; font-style:italic;">        
  35.             c# example - datetime difference in milliseconds  
  36.         </h2>        
  37.         <hr width="550" align="left" color="Gainsboro" />        
  38.         <asp:Label         
  39.             ID="Label1"         
  40.             runat="server"        
  41.             Font-Size="Large"      
  42.             Font-Names="Comic Sans MS"  
  43.             >        
  44.         </asp:Label>        
  45.         <br /><br />      
  46.         <asp:Button         
  47.             ID="Button1"         
  48.             runat="server"         
  49.             Text="get milliseconds difference between two datetime"        
  50.             OnClick="Button1_Click"      
  51.             Height="40"        
  52.             Font-Bold="true"        
  53.             />        
  54.     </div>        
  55.     </form>        
  56. </body>        
  57. </html>  








No comments:

Post a Comment