user2380981
user2380981

Reputation: 1

Find time difference between 10.30am to 7.30pm

I have two textboxes in which the user will enter startTime and endTime in 12 hours format (Eg. 10:30 AM to 7:30 PM).

On clicking the Calculate button, the user should get the difference between the two times inputted in textbox.

I have not getting what datatype should handle this inputted text.

I would really appreciate if someone could write few lines of program for me to understand better.

Upvotes: 0

Views: 1564

Answers (2)

Bilal Hashmi
Bilal Hashmi

Reputation: 1485

I see that you joined today, Welcome to Stackoverflow. Here is what you should try: Parse both Time using DateTime.ParseExact and then calculate their difference like:

DateTime  dt1 = DateTime.ParseExact("10:30 AM","h:mm tt", CultureInfo.InvariantCulture);
DateTime dt2 = DateTime.ParseExact("7:30 PM", "h:mm tt", CultureInfo.InvariantCulture);
TimeSpan difference = dt2 - dt1;

Console.WriteLine(difference.TotalHours);
Console.WriteLine(difference.TotalMinutes);

You should put what you have tried in your question and where are you stuck at. You may see http://www.whathaveyoutried.com/

Upvotes: 2

Matthew Watson
Matthew Watson

Reputation: 109567

Firstly parse the textboxes into DateTime values called date1 and date2 using DateTime.ParseExact().

Then calculate the difference using TimeSpan delta = date2 - date1;

But what have you tried so far? You need to put more effort into trying things before posting here.

Upvotes: 0

Related Questions