Getting Time Span Between Two Times in C#

Getting time span between two times in C#?

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".

Calculating how many minutes there are between two times

Try this

DateTime startTime = varValue
DateTime endTime = varTime

TimeSpan span = endTime.Subtract ( startTime );
Console.WriteLine( "Time Difference (minutes): " + span.TotalMinutes );

Edit:
If are you trying 'span.Minutes', this will return only the minutes of timespan [0~59], to return sum of all minutes from this interval, just use 'span.TotalMinutes'.

How to know if current time is between two timespans?

You can just use:

if (startTime < now && now < endTime)

Note that:

  • This doesn't check the date; doesn't look like that's an issue here
  • Depending on why you're doing this, you may want to consider intervals such as "10pm-2am" at which point you effectively want to reverse the logic
  • In most cases, it's worth making the lower-bound inclusive and the upper-bound exclusive, e.g.

    if (startTime <= now && now < endTime)

    That's useful because then you can have several intervals where the end of one interval is the start of the next interval, and any one time is in exactly one interval.

To handle the "10pm-2am" example, you'd want something like:

if (interval.StartTime < interval.EndTime)
{
// Normal case, e.g. 8am-2pm
return interval.StartTime <= candidateTime && candidateTime < interval.EndTime;
}
else
{
// Reverse case, e.g. 10pm-2am
return interval.StartTime <= candidateTime || candidateTime < interval.EndTime;
}

How do I get the time difference between two DateTime objects using C#?

The following example demonstrates how to do this:

DateTime a = new DateTime(2010, 05, 12, 13, 15, 00);
DateTime b = new DateTime(2010, 05, 12, 13, 45, 00);
Console.WriteLine(b.Subtract(a).TotalMinutes);

When executed this prints "30" since there is a 30 minute difference between the date/times.

The result of DateTime.Subtract(DateTime x) is a TimeSpan Object which gives other useful properties.

How to calculate difference between two times?

Parse both of the textboxes values to TimeSpan and then you can take their difference using -.

TimeSpan ts1 = TimeSpan.Parse(textBox1.Text); //"1:35"
TimeSpan ts2 = TimeSpan.Parse(textBox2.Text); //"3:30"

label.Text = (ts2 - ts1).ToString(); //"1:55:00"

C# check timespan between two timespans

I think this matches your requirement.

If dalEnd is lesser than dalStart, It should be the TimeSpan of the nextday.

    public Boolean CalculateDalUren(DateTime datum, TimeSpan? dalStart, TimeSpan? dalEnd)
{
Boolean isDal = false;
DateTime StartDate = DateTime.Today;
DateTime EndDate = DateTime.Today;

//Check whether the dalEnd is lesser than dalStart
if (dalStart >= dalEnd)
{
//Increase the date if dalEnd is timespan of the Nextday
EndDate = EndDate.AddDays(1);
}

//Assign the dalStart and dalEnd to the Dates
StartDate = StartDate.Date + dalStart.Value;
EndDate = EndDate.Date + dalEnd.Value;

if ((datum >= StartDate) && (datum <= EndDate))
{
isDal = true;
}
return isDal;
}

try it.

Getting time between two times with time span of one hour

I think you need something like;

DateTime dt1 = DateTime.Parse("09:00 AM");
DateTime dt2 = DateTime.Parse("06:00 PM");
while (dt1 <= dt2)
{
Console.WriteLine(dt1.ToString("hh:mm tt"));
dt1 = dt1.AddHours(1);
}

Output will be;

09:00 AM
10:00 AM
11:00 AM
12:00 PM
01:00 PM
02:00 PM
03:00 PM
04:00 PM
05:00 PM
06:00 PM

Here's a demonstration.

I'm sorry but I don't understand why you interested in TimeSpan on this case. It is a duration in time. You need to get every hour times with DateTime.

Getting time difference between two values

Use TimeSpan class, and Subtract method of DateTime.

        DateTime t1 = Convert.ToDateTime(textBox1.Text);
DateTime t2 = Convert.ToDateTime(textBox2.Text);
TimeSpan ts = t1.Subtract(t2);

How do I get TimeSpan in minutes given two Dates?

TimeSpan span = end-start;
double totalMinutes = span.TotalMinutes;

Showing Difference between two datetime values in hours

I think you're confused because you haven't declared a TimeSpan you've declared a TimeSpan? which is a nullable TimeSpan. Either remove the question mark if you don't need it to be nullable or use variable.Value.TotalHours.



Related Topics



Leave a reply



Submit