You must Sign In to post a response.
  • Category: ASP.NET

    If a employee login to office at 12:04:00 o clock and he is logout at 11:45:00

    if a employee login to office at 12:04:00 o clock and he is logout at 11:45:00

    string constr = ConfigurationManager.AppSettings["s"].ToString();
    using (SqlConnection conn = new SqlConnection(constr))
    using (SqlCommand cmd = new SqlCommand("SELECT CONVERT(VARCHAR(10),InTime,108 ), CONVERT(VARCHAR(10),outTime,108 ) FROM aten WHERE Id = '" + TextBox1.Text + "'", conn))
    SqlDataReader sdr = cmd.ExecuteReader();

    TimeSpan i = new TimeSpan(0, 0, 0);
    while (sdr.Read())
    { DateTime outTime = Convert.ToDateTime(sdr[1].ToString(), new CultureInfo("en-Gb"));
    i += outTime.Subtract(inTime);
    this.TextBox5.Text = i.ToString();


    i got the output as -00:19:00

    if a employee login at 09:10:00 Am and he logout at 09:00:00 PM
    so he id worked for 12.10 min

    if a employee login at 10:41:00 Am and he logout at 11:45:00 AM
    so he id worked for 1.04 min

    but i got the result as -01:33:00

    i need total duration as 12.10+1.04+00.19=13.33. how to calculate the total hours of the employee when login time is

    greater than logout time. and logout time is greater than login time .using the above query i got negative result
  • #750354

    Hai selvi,
    There could be 2 solutions for this-
    1-. use 24 hr clock so that you will not get the confusion for these kind of time and then you can get the correct values.
    2. Check, if you are getting the result as -ve, then add 12 hrs to it to get the correct values.
    Hope it will be helpful to you.

    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)

  • #750355

  • #750356
    Hello selvi,

    Either You should use 24 hours format or you should use AM/PM with hours.
    Sql didn't recognize what do you want AM/PM
    you can use query like that
    select datediff(MINUTE,'12:04 PM','11:45 PM') as [Minute]

    I hope you got my point, If you still have any issue than i'll get back to you.

    Nirav Prabtani (Senior Web Developer)
    Email :
    blog :

  • #750359
    please share the code .

  • #750371

  • #750407

    Along with the above Awasthi and Prabtani said, you can use Date too. When you come at night (after 24 hours) you will got the same problem. Hence you must calculate the Login and Logout with current date.

    Gopi A.
    +91 9894315571

  • #750684
    Hi Selvi,

    For this purpose, you can either use a 24 hr clock setting or even you can use the AM/PM explicitly.


    for more details, you can visit the below link-

    Ashutosh Jha

  • Sign In to post your comments