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.

Wednesday, August 6, 2014

Datetime difference in seconds

DateTime Difference:

Time difference refer to difference between two time stamp. some time we want to this for display time on the web page for example online Exam web site take this type of requirement or when user need to count login time and logout time and then calculate the time difference in which the visitor was open the web site.

Define Date time object in to

   DateTime now = DateTime.Now;  
DateTime is a class and now is the object of this class.

How to Add Minutes in datetime variable

For this we use fallowing method.
AddMinutes(<give the minutes here>);  

How to get timestamp:

TimeSpan ts = dateAfter5Minutes – now;
Here TimeSpan is type of class and ts is the object of this class.

Example of difference in seconds

In this Example we get the current time and add 5 munities in the current time and then display in an label control.

Code in :

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

<!DOCTYPE html>

<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)
        DateTime now = DateTime.Now;

        Label1.Text = "now : " + now.ToString();

        DateTime dateAfter5Minutes = now.AddMinutes(5);

        TimeSpan ts = dateAfter5Minutes - now;
        int seconds = (int)ts.TotalSeconds;

        Label1.Text += "<br ><br />after ten minutes: ";
        Label1.Text += dateAfter5Minutes.ToString();

        Label1.Text += "<br ><br />difference after adding into datetime object : ";
        Label1.Text += seconds;
<html xmlns="">
<head  runat="server">
    <title> difference in seconds</title>
    <form id="form2" runat="server">
        <h2 style="color: MidnightBlue; font-style: italic;">
            datetime difference Example in Programming</h2>
        <asp:Label ID="Label1" runat="server" Font-Size="Large" Font-Names="Comic Sans MS">       
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="get current datetime"
            OnClick="Button1_Click" Height="40" Font-Bold="true" />

Other programming related Post:

No comments:

Post a Comment