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
-2
votes
2 answers

Is there a more elegant way of removing seconds from a TimeSpan?

Assuming I have a TimeSpan that is equal to 07:10:30, I can use the following code to strip the seconds from it: myTimeSpan = new TimeSpan(myTimeSpan.Hours, myTimeSpan.Minutes, 0) Is this appropriate, or is there a more elegant way to achieve this?
Izacch
  • 383
  • 3
  • 10
-2
votes
2 answers

Get DateTime diff between 2 times

I want to select and save the entry and exit process according to the time coming from the personnel card reader. How can I get difference between 2 dates? Shift start: 07:00 end: 16:00 Shift start: 16:00 end: 23:00 Shift start: 23:00 end:…
-2
votes
1 answer

How can I create a time range with minutes?

I have this function that returns a range of times (between two TimeSpan ) by incrementing an hour. public static IEnumerable < TimeSpan > Range(TimeSpan start, TimeSpan end) { for (var dt = start; dt <= end; dt = dt.Add(new TimeSpan(1, 0,…
-2
votes
2 answers

How to Fix Time String in c#

I have a situation where in I have time strings like 10:20:70 11:65:40 I need to convert them into proper time in hh:mm:ss format using c# console. For eg : 10:20:70 will be 10:21:10 after fixing 26:12:20 will be 02:12:10 as 26hours to be considered…
-2
votes
1 answer

how to calculate years and month from given number c#

I need to calculate years and months from a given number. how can I do it? eg: I am giving: 26 I need to get result: 2 years 2months please help
-2
votes
1 answer

Find adjacent CSV rows whose timestamps exceed a given duration

I have a file with the following: "BatchID","BatchName","JobName","ScannerID","DateCreated","DailyCounter" "12713","9001","fst100","09 ","2/6/2020 6:21:06 AM","1" "12714","9002","fst100","09 ","2/6/2020 6:21:42…
Robert
  • 11
  • 2
-2
votes
1 answer

Miliseconds in C# - long or int?

I'm working with Milisecond and I've used it like Timeout = (int)TimeSpan.FromMinutes(TimeoutVal).TotalMilliseconds But I read on few places people are casting it to long instead of int? Why is that?
Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102
-2
votes
2 answers

Difference between two dates - Cannot Convert type 'int?' to 'System.TimeSpan'

I'm trying to get the difference between two dates, but I get this error: Cannot Convert type 'int?' to 'System.TimeSpan' Then I tried to use Convert.ToInt32() or Int32.Parse(), but still had the same error. Can anyone please help me or point me…
The First
  • 139
  • 2
  • 13
-2
votes
1 answer

C# calculating the time remaining until a provided string in milliseconds

My problem is the following, I pull a string of raw data from a website and it contains a value in milliseconds of a time in the future. Now I want to calculate how much time is remaining until that time in the future. First I tried converting the…
-2
votes
2 answers

invalid timespan for time string in C#

I'm trying to add a string that represents a time to a c# datetime object but I'm getting a exception that says 'invalid format' details.UTCEventDate.Add(TimeSpan.Parse(details.UTCEventTime)); where 'details.UTCEventTime' is something like "4:45AM"
chuckd
  • 13,460
  • 29
  • 152
  • 331
-2
votes
2 answers

Adding TimeSpan only to Time of Date and not Date

I have 2 DateTime's: DateTime beginDate = 2000/01/01 14:00 DateTime endDate = 2000/01/01 14:30 I calculate a timespan between these 2 hours: TimeSpan span = endDate.Subtract(beginDate); var myValue = beginDate.AddMinutes( span.Minutes…
user1702369
  • 1,089
  • 2
  • 13
  • 31
-2
votes
1 answer

StopWatch in C# to calculate inactivity time

I've got the code: public static Stopwatch stopWatch = new Stopwatch(); private void start_Checker() { stopWatch.Start(); while (true) { TimeSpan ts = stopWatch.Elapsed; if (ts.Minutes % 15…
P Sawicki
  • 189
  • 1
  • 2
  • 9
-2
votes
1 answer

How do i save time from dropdownlist to database?

my dropdown is like this here iam storing selected value of dropdown into variable of type 'TimeSpan' and saving in database. But it gives exception: 'String was not recognized as a valid TimeSpan.' my database fieldtype is also Time(7) DateTime…
ahmed
  • 39
  • 1
  • 8
-2
votes
2 answers

How do I get TimeSpan given a date and a time

Is it possible to get the timespan of A and B, where A(MM-dd-yyyy) = 10-06-2015 23:45 and B = 9:00 AM. The given format of data are exactly what it is on this post. I guess am having trouble on formatting, no coded work yet. I have other question…
pruuylan
  • 86
  • 1
  • 1
  • 10
-2
votes
3 answers

Get time span value in hh:mm format to a string variable

DateTime date1 = Convert.ToDateTime('2015/06/20'); DateTime date2= Convert.ToDateTime('2015/05/20'); TimeSpan latetime = date1.Subtract(date2);//here in 'hh:mm:ss' format string value=latetime.ToString(); I get value as in hh:mm:ss format.But I…
Safeena
  • 395
  • 2
  • 7
  • 21