timespan

Add timespan to another timespan does not work [duplicate]

↘锁芯ラ 提交于 2019-12-04 06:24:21
问题 This question already has answers here : Why isn't my TimeSpan.Add() working? (3 answers) Closed 4 years ago . I have two timespans and I want to add the second timespan to the first timespan: TimeSpan weeklyWorkTimeHours = new TimeSpan(0,0,0); TimeSpan? completeWorkTimeForCurrentDay = CalculateCompleteWorktime(currentWorkTimeItem).Value; /* I debugged through the code. This method returns a correct timespan with a correct value */ weeklyWorkTimeHours.Add(completeWorkTimeForCurrentDay.Value);

display timespan nicely

空扰寡人 提交于 2019-12-04 02:10:52
Excuse the rough code, I'm trying to display the duration of videos given the time in seconds. I've had a go below but it's not working properly. I want it to just display nicely - i.e should display 9m:59s not 09m:59s. If hours are zero dont display hours, if minutes are zero dont display minutes. public static string GetTimeSpan(int secs) { TimeSpan t = TimeSpan.FromSeconds(secs); string answer; if (secs < 60) { answer = string.Format("{0:D2}s", t.Seconds); } else if (secs < 600)//tenmins { answer = string.Format("{0:m}m:{1:D2}s", t.Minutes, t.Seconds); } else if (secs < 3600)//hour { answer

Adding a TimeSpan to a given DateTime

放肆的年华 提交于 2019-12-03 22:14:36
I just want to add 1 day to a DateTime . So I wrote: DateTime date = new DateTime(2010, 4, 29, 10, 25, 00); TimeSpan t = new TimeSpan(1, 0, 0, 0); date.Add(t); Console.WriteLine("A day after the day: " + date.ToString()); I thought the result would be: 2010 04 30- 10:25:00 but I'm still getting the initial date. What's wrong? DateTime values are immutable . The Add method returns a new DateTime value with the TimeSpan added. This works: Console.WriteLine("A day after the day: " + date.Add(t).ToString()); You need to change a line: date = date.Add(t); dtb is right about DateTime being immutable

Parse string to TimeSpan

六眼飞鱼酱① 提交于 2019-12-03 18:00:33
问题 I have some strings of xxh:yym format where xx is hours and yy is minutes like "05h:30m". What is an elegant way to convert a string of this type to TimeSpan? 回答1: This seems to work, though it is a bit hackish: TimeSpan span; if (TimeSpan.TryParse("05h:30m".Replace("m","").Replace("h",""), out span)) MessageBox.Show(span.ToString()); 回答2: DateTime.ParseExact or DateTime.TryParseExact lets you specify the exact format of the input. After you get the DateTime , you can grab the DateTime

What is Best way to calculate time span

末鹿安然 提交于 2019-12-03 16:30:36
In my c# program, my requirement is to calculate a timespan for business logic execution that is inside a foreach loop I have to store time span. I am using following code for (int i = 0; i < 100; i++) { DateTime start= DateTime.Now; // Business logic DateTime end= DateTime.Now; TimeSpan time = start.Subtract(end); // Save timespan in log file } Please correct me whether I am using right code, or do I need to modify for better performance and result. You should use a Stopwatch . The Stopwatch is much more accurate for time measurement than the wall time clock. var sw = Stopwatch.StartNew(); //

Opening Hours Database Design

房东的猫 提交于 2019-12-03 14:53:28
问题 We are currently developing an application in which multiple entities have associated opening hours. Opening hours may span multiple days, or may be contained within a single day. Ex. Opens Monday at 6:00 and closes at Friday at 18:00. Or Opens Monday at 06:00 and closes Monday at 15:00. Also, an entity may have multiple sets of opening hours per day. So far, the best design I have found, is to define an opening hour to consist of the following: StartDay, StartTime, EndDay and EndTime. This

remove seconds from timespan using c#

假如想象 提交于 2019-12-03 13:14:52
I want to remove the seconds from timespan using c# My code is here: TimeSpan lateaftertime = new TimeSpan(); lateaftertime = lateafter - Convert.ToDateTime(intime) ; It returns the value 00:10:00 But i want the below output : 00:10 only not seconds field :00 . Well you can simply do as string.Format("{0}:{1}", ts.Hours,ts.Minutes) // it would display 2:5 EDIT to get it properly formatted use string.Format("{0:00}:{1:00}", ts.Hours,ts.Minutes) // it should display 02:05 Note that a TimeSpan does not have a format . It's stored in some internal representation which does not resemble 00:10:00 at

How long is a .NET DateTime/TimeSpan tick?

懵懂的女人 提交于 2019-12-03 11:27:38
问题 How long is a .NET DateTime/TimeSpan tick? 回答1: Although currently a tick is 100 nanoseconds, it is best not to rely on this as an absolute. Rather, use ' TimeSpan.TicksPerSecond ' (or any of the other TicksPerXxx member values) 回答2: The tick is the unit of granularity for the .NET DateTime and TimeSpan value types. It has the following common conversions: 1 tick = 100 nanoseconds = 0.1 microseconds = 0.0001 milliseconds = 0.0000001 seconds 回答3: To quote MSDN: The smallest unit of time is the

how to perform division in timespan [duplicate]

安稳与你 提交于 2019-12-03 10:46:33
This question already has answers here : What is the best way to divide two TimeSpan objects? (4 answers) 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 Please tell me how I can divide tsp1 by tsp2 so that I can get the exact number of times tsp2 divides into tsp1 and what the remainder is. I am using Visual Studio 2008. Thanks. The simplest approach is probably just to take their lengths in ticks, and divide those. For example: long ticks1 = tsp1.Ticks; long ticks2 = tsp2.Ticks; long

Convert a time span in seconds to formatted time in shell

天大地大妈咪最大 提交于 2019-12-03 08:14:47
问题 I have a variable of $i which is seconds in a shell script, and I am trying to convert it to 24 HOUR HH:MM:SS. Is this possible in shell? 回答1: Here's a fun hacky way to do exactly what you are looking for =) date -u -d @${i} +"%T" Explanation: The date utility allows you to specify a time, from string, in seconds since 1970-01-01 00:00:00 UTC, and output it in whatever format you specify. The -u option is to display UTC time, so it doesn't factor in timezone offsets (since start time from