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
238
votes
10 answers

What is the correct SQL type to store a .Net Timespan with values > 24:00:00?

I am trying to store a .Net TimeSpan in SQL server 2008 R2. EF Code First seems to be suggesting it should be stored as a Time(7) in SQL. However TimeSpan in .Net can handle longer periods than 24 hours. What is the best way to handle storing…
GraemeMiller
  • 11,973
  • 8
  • 57
  • 111
233
votes
8 answers

Showing Difference between two datetime values in hours

I am retrieving two date time values from the database. Once the value is retrieved, I need the difference between the two values. For that, I create a timespan variable to store the difference of the 2 date values. TimeSpan? variable = datevalue1 -…
reggie
  • 13,313
  • 13
  • 41
  • 57
215
votes
13 answers

How to serialize a TimeSpan to XML

I am trying to serialize a .NET TimeSpan object to XML and it is not working. A quick google has suggested that while TimeSpan is serializable, the XmlCustomFormatter does not provide methods to convert TimeSpan objects to and from XML. One…
joeldixon66
  • 2,153
  • 2
  • 13
  • 5
192
votes
12 answers

Find if current time falls in a time range

Using .NET 3.5 I want to determine if the current time falls in a time range. So far I have the currentime: DateTime currentTime = new DateTime(); currentTime.TimeOfDay; I'm blanking out on how to get the time range converted and compared. Would…
John M
  • 14,338
  • 29
  • 91
  • 143
149
votes
9 answers

C# 4.0: Can I use a TimeSpan as an optional parameter with a default value?

Both of these generate an error saying they must be a compile-time constant: void Foo(TimeSpan span = TimeSpan.FromSeconds(2.0)) void Foo(TimeSpan span = new TimeSpan(2000)) First of all, can someone explain why these values can't be determined at…
Mike Pateras
  • 14,715
  • 30
  • 97
  • 137
147
votes
27 answers

calculating the difference in months between two dates

In C#/.NET TimeSpan has TotalDays, TotalMinutes, etc. but I can't figure out a formula for total months difference. Variable days per month and leap years keep throwing me off. How can I get TotalMonths? Edit Sorry for not being more clear: I know I…
Dinah
  • 52,922
  • 30
  • 133
  • 149
144
votes
7 answers

Measuring code execution time

I want to know how much time a procedure/function/order takes to finish, for testing purposes. This is what I did but my method is wrong 'cause if the difference of seconds is 0 can't return the elapsed milliseconds: Notice the sleep value is 500 ms…
ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
141
votes
14 answers

How do I convert a TimeSpan to a formatted string?

I have two DateTime vars, beginTime and endTime. I have gotten the difference of them by doing the following: TimeSpan dateDifference = endTime.Subtract(beginTime); How can I now return a string of this in hh hrs, mm mins, ss secs format using…
Michael Wheeler
  • 2,459
  • 3
  • 19
  • 26
104
votes
7 answers

How do I convert ticks to minutes?

I have a ticks value of 28000000000 which should be 480 minutes but how can I be sure? How do I convert a ticks value to minutes?
Ben H
95
votes
9 answers

Multiply TimeSpan in .NET

How do I multiply a TimeSpan object in C#? Assuming the variable duration is a TimeSpan, I would like, for example duration*5 But that gives me an error "operator * cannot be applied to types TimeSpan and int". Here's my current…
Colonel Panic
  • 132,665
  • 89
  • 401
  • 465
95
votes
4 answers

Merge pandas dataframes where one value is between two others

I need to merge two pandas dataframes on an identifier and a condition where a date in one dataframe is between two dates in the other dataframe. Dataframe A has a date ("fdate") and an ID ("cusip"): I need to merge this with this dataframe B: on…
itzy
  • 11,275
  • 15
  • 63
  • 96
86
votes
10 answers

TimeSpan to DateTime conversion

I want to convert a Timespan to Datetime. How can I do this? I found one method on Google: DateTime dt; TimeSpan ts="XXX"; //We can covnert 'ts' to 'dt' like this: dt= Convert.ToDateTime(ts.ToString()); Is there any other way to do this?
Raghav55
  • 3,055
  • 6
  • 28
  • 38
78
votes
8 answers

How to get the latest and oldest record in mongoose.js (or just the timespan between them)

Basic problem I have a bunch of records and I need to get latest (most recent) and the oldest (least recent). When googling I found this topic where I saw a couple of queries: // option 1 Tweet.findOne({}, [], { $orderby : { 'created_at' : -1 } },…
askmike
  • 1,900
  • 4
  • 21
  • 27
77
votes
12 answers

Convert milliseconds to human readable time lapse

I would like to format some commands execution times in a human readable format, for example: 3 -> 3ms 1100 -> 1s 100ms 62000 -> 1m 2s etc .. Taking into account days, hours, minutes, seconds, ... Is it possible using C#?
Daniel Peñalba
  • 30,507
  • 32
  • 137
  • 219
76
votes
6 answers

C# Timespan Milliseconds vs TotalMilliseconds

In the example below, why does the Milliseconds property return 0 but the TotalMilliseconds property return 5000? // 5 seconds TimeSpan intervalTimespan = new TimeSpan(0, 0, 5); // returns 0 intervalTimespan.Milliseconds; // returns…
AJM
  • 32,054
  • 48
  • 155
  • 243
1
2 3
78 79