tagged [timespan]

Why isn't my TimeSpan.Add() working?

Why isn't my TimeSpan.Add() working? There has to be an easy answer: There's only one value in the list `timeSheets` and it is a positive `TimeSpan` (verified on local inspection).

9 Aug at 19:18

C# best way to compare two time of the day

C# best way to compare two time of the day I woulld like to know if a specified time of the day is passed. I don't really like the way I am doing: How do you do?

23 Oct at 09:52

Format TimeSpan greater than 24 hour

Format TimeSpan greater than 24 hour Say I convert some seconds into the TimeSpan object like this: How do I format the TimeSpan object into a format like the following: Is there a built-in function o...

23 Jan at 10:32

Random DateTime between range - not unified output

Random DateTime between range - not unified output I implemented the below RandomDate, but I always keep getting values closed to "From" date, i probably miss something here.... ``` public static Date...

24 Jan at 16:20

Why does TimeSpan.ParseExact not work

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! The second parse fails with an exception "Input string was not in a ...

20 Aug at 13:5

How do I convert a TimeSpan to a formatted string?

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: How can I now return a string of this i...

2 Oct at 15:6

how to perform division in timespan

how to perform division in timespan I have a value in `TimeSpan`, let's say: `tsp1` = 2 hour 5 minutes. I have another `TimeSpan` variable which contains a value like: `tsp2` = 0 hours 2 minutes Pleas...

30 Sep at 15:13

Adding a TimeSpan to a given DateTime

Adding a TimeSpan to a given DateTime I just want to add 1 day to a `DateTime`. So I wrote: I thought the result would be: `2010 04 30- 10:25:00` but I'm still getting the initial date. What's wrong?

16 Aug at 07:39

TimeSpan to friendly string library (C#)

TimeSpan to friendly string library (C#) Does anyone know of a good library (or code snippet) for converting a TimeSpan object to a "friendly" string such as: - - (It's for a document expiry system, w...

6 May at 13:32

Multiply TimeSpan in .NET

Multiply TimeSpan in .NET How do I multiply a TimeSpan object in C#? Assuming the variable `duration` is a [TimeSpan](http://msdn.microsoft.com/en-us/library/system.timespan.aspx), I would like, for e...

11 Jan at 16:36

Datetime.now as TimeSpan value?

Datetime.now as TimeSpan value? I need the current Datetime minus `myDate1` in seconds. . . I tried different ways to calculate but to no effect. ``` TimeSpan mySpan = new TimeSpan(myDate2.Day, myDate...

21 Oct at 13:44

Find average of collection of TimeSpans

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

5 Jan at 09:34

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

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: First of all, can someone explain why these val...

Find if current time falls in a time range

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: I'm blanking out on how to get the time range con...

27 Feb at 19:20

Sum of TimeSpans in C#

Sum of TimeSpans in C# I have a collection of objects that include a TimeSpan variable: I want to use LINQ to sum those times. Of course, (from r in MyCollection select r.TheDuration).Sum(); doesn't w...

15 Jun at 14:50

Servicestack redis client: setting a key with timespan expiration fails using

Servicestack redis client: setting a key with timespan expiration fails using The following sample fails when setting a key with timespan expiration. When setting the expiration as a datetime, it pass...

17 Aug at 12:21

Using LINQ to do some calculations on the current and the next object

Using LINQ to do some calculations on the current and the next object Is there an elegant solution to walk through an ordered list to do some calculations on the current and the next object? There mus...

29 Jul at 19:49

TimeSpan to Custom string like HH:mm:ss

TimeSpan to Custom string like HH:mm:ss I would like to convert to string like `00:00:01` I try this `delta.ToString(@"0:\\hh\\:mm\\:ss", System.Globalization.CultureInfo.InvariantCulture);` But nothi...

9 Jul at 19:55

Timespan formatting

Timespan formatting How do you elegantly format a timespan to say example "1 hour 10 minutes" when you have declared it as : ? I am of course aware that you could do some simple maths for this, but I ...

2 Oct at 15:6

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

Calculate the difference between two dates and get the value in years? > [How do I calculate someone’s age in C#?](https://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c) I want ...

23 May at 11:47

How to format a TimeSpan for hours not days

How to format a TimeSpan for hours not days The following code produces this output: > 0 hours 0 minutes What I would like is this output: > 24 hours 0 minutes What am I missing in this format string?...

18 Apr at 14:1

Handle negative time spans

Handle negative time spans In my output of a grid, I calculate a `TimeSpan` and take its `TotalHours`. e.g. The goal is to show the `TotalHours` as `39:44`, so I need to convert the value from `7.5` t...

5 Oct at 14:45

TimeSpan FromMilliseconds strange implementation?

TimeSpan FromMilliseconds strange implementation? I recently encountered some weird behaviour in the .NET `TimeSpan` implementation. `FromMilliseconds` takes a double as parameter. However, it seems t...

23 Jan at 19:41

Why use TimeSpan.CompareTo() rather than < > or =

Why use TimeSpan.CompareTo() rather than or = I've been going over some Microsoft samples of code for the Kinect sensor and have stumbled across the following line. I understand how `CompareTo()` is u...

15 Apr at 16:4

Environment.TickCount vs DateTime.Now

Environment.TickCount vs DateTime.Now Is it ever OK to use `Environment.TickCount`to calculate time spans? Because `TickCount` is signed and will rollover after 25 days (it takes 50 days to hit all 32...

5 Jan at 20:44