c#

How to calculate time difference in c#?

Ans:

In some cynerio we need difference between two times. For ex. In corporate sector employees in time and out time we mensioned then we need how many hours the employee works in the office. In this situation we need differences between two timings.

Ex:

string startTime = “7:00 AM”;
string endTime = “2:00 PM”;

TimeSpan duration = DateTime.Parse(endTime).Subtract(DateTime.Parse(startTime));

Console.WriteLine(duration);
Console.ReadKey();

it will generate the output as 7 hours.

Leave a comment

Your email address will not be published. Required fields are marked *