简体   繁体   中英

How do I get the total time difference between two DateTime using C#?

As you can see below I declared the queries and DB operations required for the application. In this class I tried to calculate total time with TimeSpan but was unsuccessful.

public class Operations
{
    public Dbconection db = new Dbconection();
    public Informations info = new Informations();

    DateTime Vrijeme;
    DateTime myDate1;

    public int insertEmp(Informations info)
    {
        DateTime Time = DateTime.Now;
        DateTime myDate1 = new DateTime(Time.Year, Time.Month, 
            Time.Day, Time.Hour, 10, 00);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "INSERT INTO empRegister" +
            "(Operater,Artikl,Kal,Bazz,Tempa,Time,Status)" +
            "VALUES ('" + info.operater + "','" + info.artikl + "','" + info.kal + "','" + info.bazz + "','" + info.tempa + "', @time, 'online')";
        cmd.Parameters.AddWithValue("@time", myDate1);
        return db.ExeNonQuery(cmd);
    }

    public DataTable Logout1(Informations info)
    {
        DateTime Logout = DateTime.Now;
        DateTime myDate2 = new DateTime(Logout.Year, Logout.Month, Logout.Day, Logout.Hour, 30, 00);

        TimeSpan ts = myDate2.Subtract(myDate1);
        TimeSpan result = new TimeSpan(ts.Hours, ts.Minutes, ts.Milliseconds);

        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;

        cmd.CommandText = "update empRegister set Logout = @logout, Total = @THETIME, Status = 'logout' where Operater ='" + info.operater + "' and Status = 'online'";
        cmd.Parameters.AddWithValue("@logout", myDate2);
        cmd.Parameters.AddWithValue("@THETIME", SqlDbType.Time);
        cmd.Parameters["@THETIME"].Value = result;

        return db.ExeReader(cmd);
    }
}

With insert button I want use Datetime.Now and with update button I want to store the current time and total time of two DateTime with Timespan . I am using a SQL DB.

I'm not a big fan of storing the time data. However, you can do it in the SQL query assuming you are storing it as a time column:

cmd.CommandText = "update empRegister set Logout = @logout, Total = CONVERT(time(7),DATEADD(SECOND, 
                        DATEDIFF(SECOND, Time, @logout),
                        0)), Status = 'logout' where Operater = @Operater and Status = 'online'";
cmd.Parameters.AddWithValue("@logout", myDate2);
cmd.Parameters.AddWithValue("@Operater", info.operater);

None of the TimeSpan code is necessary.

Also, I made the operator a parameter. It's misspelled in your question and in my query for consistency.

Here's a SQL Fiddle example of a time column use:

create table y
(
  x time
  );

INSERT INTO Y SELECT 
CONVERT(time(7),DATEADD(SECOND, 
                        DATEDIFF(SECOND, GETDATE()-7.5, GETDATE()),
                        0));


SELECT * FROM Y

Note that the time column is limited to 24 hours.

In the empRegister table I have stored 3 Data Type with names (Time,Logout and Total). I do not know where I'm wrong and every help is welcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM