## 7 replies

Last post Sep 14, 2013 03:42 AM by leveena

• prk_in

Contributor

4046 Points

1099 Posts

### Find time difference between 01.30pm to 11.30pm Using c#.net

Ho  to find out the Time Difference ( Difference will include HH:MM) Between the 01.30pm to 11.30pm using the C#.net ?

Findtimedifferencebetween01.30pmto11.30pmUsingc#.net FindtimedifferencebetweenUsingc#.net GettingtimedifferencewithAM asp.netCalculateTimeDifferenceinC#

Happy Coding !
Please Mark If it helps you || Ramakrishna.p
• Rajneesh Ver...

All-Star

28957 Points

7244 Posts

### Re: Find time difference between 01.30pm to 11.30pm Using c#.net

May 20, 2013 03:37 AM|Rajneesh Verma|LINK

#### prk_in

using the C#.net ?

Try as:

```DateTime dt1 = DateTime.ParseExact("11:30 PM" , "HH:mm tt", new DateTimeFormatInfo());
DateTime dt2 = DateTime.ParseExact("01:30 PM", "HH:mm tt", new DateTimeFormatInfo());
TimeSpan ts = dt1.Subtract(dt2);```

Findtimedifferencebetween01.30pmto11.30pmUsingc#.net FindtimedifferencebetweenUsingc#.net GettingtimedifferencewithAM asp.netCalculateTimeDifferenceinC#

• amolpandit22

Participant

1354 Points

459 Posts

### Re: Find time difference between 01.30pm to 11.30pm Using c#.net

Use below sample code.

```DateTime d1 = new DateTime();
d1 = Convert.ToDateTime("11:30PM");

DateTime d2 = new DateTime();
d2 = Convert.ToDateTime("01:30PM");

TimeSpan ts = d1.Subtract(d2);```

Findtimedifferencebetween01.30pmto11.30pmUsingc#.net FindtimedifferencebetweenUsingc#.net GettingtimedifferencewithAM asp.netCalculateTimeDifferenceinC#

Amol S. Pandit
• prk_in

Contributor

4046 Points

1099 Posts

### Re: Find time difference between 01.30pm to 11.30pm Using c#.net

`DateTime d1 = new DateTime(); d1 = Convert.ToDateTime("03:00PM"); DateTime d2 = new DateTime(); d2 = Convert.ToDateTime("09:30PM"); TimeSpan ts = d1.Subtract(d2);When i was tried like i was getting time difference like -6:-30hrs , i need to remove negative sign values how can i achive this .`

Findtimedifferencebetween01.30pmto11.30pmUsingc#.net FindtimedifferencebetweenUsingc#.net GettingtimedifferencewithAM asp.netCalculateTimeDifferenceinC#

Happy Coding !
Please Mark If it helps you || Ramakrishna.p
• Rion William...

All-Star

113020 Points

18361 Posts

MVP

### Re: Find time difference between 01.30pm to 11.30pm Using c#.net

May 20, 2013 11:33 AM|Rion Williams|LINK

DateTime objects can easily be compared within .NET as you can use basic arithmetic operations such as addition and subtraction between the dates to yield TimeSpan objects, which have properties that can yield the information that you need :

```//Your DateTime values
DateTime startDateTime = DateTime.Parse("1:30 PM");
DateTime endDateTime = DateTime.Parse("11:30 PM");

TimeSpan difference = endDateTime - startDateTime;

//Access the necessary properties or display your difference below (you can use .Duration() to get an absolute value)
string differenceString = difference.ToString(); //yields "10:00:00"```

If you needed to access other properties such as the number of minutes, days or hours, you can access those directly through the TimeSpan object :

```//Individual available properties of a TimeSpan object
int days = difference.TotalDays;
int hours = difference.TotalHours;
int minutes = difference.TotalMinutes;```

Findtimedifferencebetween01.30pmto11.30pmUsingc#.net FindtimedifferencebetweenUsingc#.net GettingtimedifferencewithAM asp.netCalculateTimeDifferenceinC#

• Rion William...

All-Star

113020 Points

18361 Posts

MVP

### Re: Find time difference between 01.30pm to 11.30pm Using c#.net

May 20, 2013 11:35 AM|Rion Williams|LINK

#### prk_in

```DateTime d1 = new DateTime();
d1 = Convert.ToDateTime("03:00PM"); DateTime d2 = new DateTime();
d2 = Convert.ToDateTime("09:30PM");
TimeSpan ts = d1.Subtract(d2);
When i was tried like i was getting time difference like -6:-30hrs ,
i need to remove negative sign values how can i achive this .```

As I mentioned in my answer, the TimeSpan object has a method called Duration() that will yield the absolute value of a TimeSpan (basically ignoring any negative values) :

`//Duration will yield the absolute value of a comparisonTimeSpan ts = d1.Subtract(d2).Duration();`

Findtimedifferencebetween01.30pmto11.30pmUsingc#.net FindtimedifferencebetweenUsingc#.net GettingtimedifferencewithAM asp.netCalculateTimeDifferenceinC#

• Paul Linton

Star

9555 Points

2784 Posts

### Re: Find time difference between 01.30pm to 11.30pm Using c#.net

May 20, 2013 05:46 PM|Paul Linton|LINK

In your original post the string was "01.30pm" - note the separator is a '.'  All replies have then assumed that the separator was ':' and none of the replies given would work with the problem as stated in the OP.

In a later post your input is '3:00PM' - the separator is now a ':'

Which case do you wish to handle?  Or is it both?

Findtimedifferencebetween01.30pmto11.30pmUsingc#.net FindtimedifferencebetweenUsingc#.net GettingtimedifferencewithAM asp.netCalculateTimeDifferenceinC#

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
• leveena

Member

10 Points

10 Posts

### Re: Find time difference between 01.30pm to 11.30pm Using c#.net

System.TimeSpan diff = secondDate.Subtract(firstDate);

other methods to find date difference

DateTime newDate = new DateTime(2000, 5, 1);

String diff2 = (secondDate - firstDate).TotalDays.ToString();

Source http://csharp.net-informations.com/statements/csharp-date-difference.htm c# date difference

Lev.

Findtimedifferencebetween01.30pmto11.30pmUsingc#.net FindtimedifferencebetweenUsingc#.net GettingtimedifferencewithAM asp.netCalculateTimeDifferenceinC#