asp.net time lag

Source: Internet
Author: User
Tags object current time datetime net net time string tostring
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
 
 
public partial class _Default: System.Web.UI.Page
{
    protected void Page_Load (object sender, EventArgs e)
    {
        if (! IsPostBack)
        {
            string connStr = "Server = .; Database = Test; uid = sa";
            SqlConnection conn = new SqlConnection (connStr);
            SqlCommand comm = new SqlCommand ("select NowDt from Table1 where DtId = 1", conn);
            System.DateTime aa; // Time stored in the database
            System.DateTime bb; // Current time
 
            // take the time in the database, the field is of type varChar
            try
            {
                conn.Open ();
                aa = Convert.ToDateTime (comm.ExecuteScalar (). ToString ()); // Database time, convert object to string and convert to DateTime
                conn.Close ();
            }
            catch (Exception exp)
            {
                throw exp;
            }
            finally
            {
                conn.Dispose ();
                comm.Dispose ();
            }
            this.HLOldDt.Text = aa.ToString (); // Time in database
 
            bb = System.DateTime.Now; // Current time
            this.HLNowDt.Text = bb.ToString (); // output the current time
 
            //jet lag  
            string ss = this.DateDiff (aa, bb);
            this.HLSe.Text = ss.ToString (); // output time difference
        }
    }
 
    // Compared time difference
    private string DateDiff (DateTime DateTime1, DateTime DateTime2)
    {
        string dateDiff = null;
          
        try
        {
            TimeSpan ts1 = new TimeSpan (DateTime1.Ticks);
            TimeSpan ts2 = new TimeSpan (DateTime2.Ticks);
            TimeSpan ts = ts1.Subtract (ts2) .Duration (); // Find the absolute value of the time difference
 
            // Build time string
            dateDiff = ts.Days.ToString () + "day" + ts.Hours.ToString () + "hour" + ts.Minutes.ToString () + "minute" + ts.Seconds.ToString () + "second";
        }
        catch
        {
        } return dateDiff;
    }
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.