Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8557809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:39:32+00:00 2026-06-11T15:39:32+00:00

I have two textboxes. One for a clock in time and one for clock

  • 0

I have two textboxes. One for a clock in time and one for clock out. The times will be put in this format:

Hours:Minutes

Lets say I have clocked in at 7:00 AM and clocked out at 2:00 PM.

With my current code, I get a difference of 2 hours, but it should be 7 hours. How would I do that in C#. I was going to convert to the 24 hour, by letting the user select AM or PM, but I got confused.

So, basically, how would I calculate the difference of hours between the two times?

I tried this, but got 2 hours and not 7 when I plugged in the numbers.

DateTime startTime = Convert.ToDateTime(textBox1.Text);
DateTime endtime = Convert.ToDateTime(textBox2.Text);

TimeSpan duration = startTime - endtime;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:39:33+00:00Added an answer on June 11, 2026 at 3:39 pm
    string startTime = "7:00 AM";
    string endTime = "2:00 PM";
    
    TimeSpan duration = DateTime.Parse(endTime).Subtract(DateTime.Parse(startTime));
    
    Console.WriteLine(duration);
    Console.ReadKey();
    

    Will output: 07:00:00.

    It also works if the user input military time:

    string startTime = "7:00";
    string endTime = "14:00";
    
    TimeSpan duration = DateTime.Parse(endTime).Subtract(DateTime.Parse(startTime));
    
    Console.WriteLine(duration);
    Console.ReadKey();
    

    Outputs: 07:00:00.

    To change the format: duration.ToString(@"hh\:mm")

    More info at: http://msdn.microsoft.com/en-us/library/ee372287.aspx

    Addendum:

    Over the years it has somewhat bothered me that this is the most popular answer I have ever given; the original answer never actually explained why the OP’s code didn’t work despite the fact that it is perfectly valid. The only reason it gets so many votes is because the post comes up on Google when people search for a combination of the terms “C#”, “timespan”, and “between”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two asp.net textboxes and need to put a validate function in javascript
I have two aspx page on my application. From the first one, lets say
I have two ListBoxes, one inside another. And both ListBoxes will have items dynamically
I have two TextBoxes: the first one is Firstname and the other is Lastname.
A basic C# Problem. I have two disabled textboxes on my form, and one
I have two textboxes and when TAB is pressed from one textbox it goes
i have one login.jsp web page where i have two textboxes for username and
Default3.aspx This is my first page. in which i have two textbox and one
I have a form with two text boxes, one select drop down and one
I have two div's one has textbox in it and other just plain text.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.