Questions tagged [timespan]

An elapsed measurement of time between two events or during which an event, program or a function continues. Also known as a timedelta or a duration.

A timespan is an elapsed measurement of time between two events or during which an event, program or a function continues.

It is also known as a timedelta or a duration in many programming languages.

  • For most intervals of time, this is calculated by:

    EndTime - StartTime
    

    This works especially well for half-open intervals that are used by time-only ranges, or date+time ranges.

  • However, be careful with date-only ranges. Typically these are expressed using fully-closed intervals, such as Jan 1 to Jan 2 to represent two days. This is calculated by:

    EndDate - StartDate + 1
    

See also the and tags.

1171 questions
72
votes
14 answers

Environment.TickCount vs DateTime.Now

Is it ever OK to use Environment.TickCountto calculate time spans? int start = Environment.TickCount; // Do stuff int duration = Environment.TickCount - start; Console.WriteLine("That took " + duration " ms"); Because TickCount is signed and will…
Jon B
  • 51,025
  • 31
  • 133
  • 161
71
votes
5 answers

Check if datetime instance falls in between other two datetime objects

I would like to know a simple algorithm to check if the given instance of datetime lies between another two instances in C#. Note: I skimmed though this How do I check if a given datetime object is "between" two datetimes? and it was for python and…
Deeptechtons
  • 10,945
  • 27
  • 96
  • 178
71
votes
32 answers

Check if a given time lies between two times regardless of date

I have timespans: String time1 = 01:00:00 String time2 = 05:00:00 I want to check if time1 and time2 both lies between 20:11:13 and 14:49:00. Actually, 01:00:00 is greater than 20:11:13 and less than 14:49:00 considering 20:11:13 is always less…
sjain
  • 23,126
  • 28
  • 107
  • 185
67
votes
9 answers

Sum of TimeSpans in C#

I have a collection of objects that include a TimeSpan variable: MyObject { TimeSpan TheDuration { get; set; } } I want to use LINQ to sum those times. Of course, (from r in MyCollection select r.TheDuration).Sum(); doesn't work! I'm thinking…
frenchie
  • 51,731
  • 109
  • 304
  • 510
65
votes
4 answers

How to Convert string "07:35" (HH:MM) to TimeSpan

I would like to know if there is a way to convert a 24 Hour time formatted string to a TimeSpan. Right now I have a "old fashion style": string stringTime = "07:35"; string[] values = stringTime.Split(':'); TimeSpan ts = new TimeSpan(values[0],…
VAAA
  • 14,531
  • 28
  • 130
  • 253
65
votes
6 answers

Getting time span between two times in C#?

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…
Hunter Mitchell
  • 7,063
  • 18
  • 69
  • 116
60
votes
3 answers

Find average of collection of TimeSpans

I have collection of TimeSpans, they represent time spent doing a task. Now I would like to find the average time spent on that task. It should be easy but for some reason I'm not getting the correct average. Here's my code: private TimeSpan?…
hs2d
  • 6,027
  • 24
  • 64
  • 103
59
votes
4 answers

Do we have a TimeSpan sort of class in Java

I was just wondering if there is a need of TimeSpan in java.util so that I can define how much hours,minutes and seconds are there in between these two times. From this TimeSpan we can have a time interval between two times. like TimeSpan…
Talha Ahmed Khan
  • 15,043
  • 10
  • 42
  • 49
56
votes
6 answers

Calculate the difference between two dates and get the value in years?

Possible Duplicate: How do I calculate someone’s age in C#? I want to calculate basically the age of employees - So we have DOB for each employee, So on the C# Side I want to do something like this - int age=Convert.Int32(DateTime.Now-DOB); I…
Vishal
  • 12,133
  • 17
  • 82
  • 128
53
votes
4 answers

How long is a .NET DateTime/TimeSpan tick?

How long is a .NET DateTime/TimeSpan tick?
Jason Kresowaty
  • 16,105
  • 9
  • 57
  • 84
52
votes
6 answers

Why does TimeSpan.ParseExact not work

This is a bit wierd. Parsing a text field with a valid timespan fails if I try to be precise! const string tmp = "17:23:24"; //works var t1 = TimeSpan.Parse(tmp); //fails var t2 = TimeSpan.ParseExact(tmp, "hh:mm:ss",…
Quango
  • 12,338
  • 6
  • 48
  • 83
49
votes
7 answers

Convert TimeSpan from format "hh:mm:ss" to "hh:mm"

I want to show in a TextBox only hour and minutes var test = dataRow.Field("fstart").ToString(); //test ="08:00:00" var tb = (TextBox) gridViewRow.Cells[2].FindControl("fstart"); tb.Text = test; how to show only hours and minutes…
Alex
  • 8,908
  • 28
  • 103
  • 157
48
votes
3 answers

TimeSpan.ToString("hh:mm") error

Why I got an error when I want to get the string of a TimeSpan with a custom format. DateTime.Now.TimeOfDay.ToString("hh:mm"); // Error: Input string was not in a correct format.
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
47
votes
9 answers

Handle negative time spans

In my output of a grid, I calculate a TimeSpan and take its TotalHours. e.g. (Eval("WorkedHours") - Eval("BadgedHours")).TotalHours The goal is to show the TotalHours as 39:44, so I need to convert the value from 7.5 to 07:30. This is no problem...…
fr0xx
44
votes
10 answers

Format TimeSpan greater than 24 hour

Say I convert some seconds into the TimeSpan object like this: Dim sec = 1254234568 Dim t As TimeSpan = TimeSpan.FromSeconds(sec) How do I format the TimeSpan object into a format like the following: >105hr 56mn 47sec Is there a built-in function…
Maxd
  • 445
  • 1
  • 4
  • 5
1
2
3
78 79