timespan

Format TimeSpan in DataGridView column

此生再无相见时 提交于 2019-11-28 00:57:19
问题 I've seen these questions but both involve methods that aren't available in the CellStyle Format value. I only want to show the hours and minutes portion (16:05); not the seconds as well (16:05:13). I tried forcing the seconds value to zero but still got something like 16:05:00. Short of using a kludge like providing a string or a DateTime (and only showing the hour/minutes part) is there any way I can get the formatting to do what I want. 回答1: I just discovered this myself. Unfortunately,

TimeSpan.Parse time format hhmmss

故事扮演 提交于 2019-11-27 21:42:08
问题 in c# i have time in format hhmmss like 124510 for 12:45:10 and i need to know the the TotalSeconds. i used the TimeSpan.Parse("12:45:10").ToTalSeconds but it does'nt take the format hhmmss. Any nice way to convert this? 回答1: This might help using System; using System.Globalization; namespace ConsoleApplication7 { class Program { static void Main(string[] args) { DateTime d = DateTime.ParseExact("124510", "hhmmss", CultureInfo.InvariantCulture); Console.WriteLine("Total Seconds: " + d

A Real Timespan Object With .Years & .Months

被刻印的时光 ゝ 提交于 2019-11-27 19:04:36
Consider the following 2 scenarios: Scenario 1). Today is May 1st 2012, and Scenario 2). Today is September 1st 2012. Now, consider that we write on our webpage the following about a comment someone has left: "This comment was written 3 months and 12 days ago". The amount of days in both these scenarios will ALWAYS be different even though the statement is exactly the same. In Scenario 1, "3 months and 12 days" would equal 102 days . However, in Scenario 2, "3 months and 12 days" would be 104 days ! Now, to corner in on my point, lets use a different example and say that someone left a comment

TimeSpan FromMilliseconds strange implementation?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 17:12:56
问题 I recently encountered some wird behaviour in the .net timespan implementation. TimeSpan test = TimeSpan.FromMilliseconds(0.5); double ms = test.TotalMilliseconds; // Returns 0 The FromMilliseconds takes a double as parameter. However, it seems the value is rounded internally. If I instantiate a new timespan with 5000 ticks (.5 ms), the value of TotalMilliseconds is correct. Looking at the TimeSpan implementation in reflector reveals that the input is in fact casted to a long. Why did

How to serialize a TimeSpan to XML

流过昼夜 提交于 2019-11-27 16:51:51
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 suggested approach was to ignore the TimeSpan for serialization, and instead serialize the result of TimeSpan.Ticks (and use new TimeSpan(ticks) for deserialization). An example of this follows: [Serializable] public class MyClass { // Local Variable private TimeSpan m_TimeSinceLastEvent; // Public Property - XmlIgnore as it doesn't serialize anyway

Timespan formatting [duplicate]

只愿长相守 提交于 2019-11-27 14:57:40
This question already has an answer here: How can I String.Format a TimeSpan object with a custom format in .NET? 19 answers How do you elegantly format a timespan to say example "1 hour 10 minutes" when you have declared it as : TimeSpan t = new TimeSpan(0, 70, 0); ? I am of course aware that you could do some simple maths for this, but I was kinda hoping that there is something in .NET to handle this for me - for more complicated scenarios Duplicate of How can I String.Format a TimeSpan object with a custom format in .NET? There is no built-in functionality for this, you'll need to use a

What is the best way to represent a timespan in SQL Server CE?

北慕城南 提交于 2019-11-27 14:27:06
问题 Specifically speaking I only need hours:minutes but say I have a .NET TimeSpan object, how should I store that in a SQL(CE) database? 回答1: I'd recommend using a long to represent the number of ticks. That's what TimeSpan uses as it's internal representation. This lets you easily reconstitute your object with the Timespan.FromTicks() method, and output to the database using the Timespan.Ticks property. The smallest unit of time in .NET is the tick, which is equal to 100 nanoseconds 回答2: Store

Why does TimeSpan.FromSeconds(double) round to milliseconds?

回眸只為那壹抹淺笑 提交于 2019-11-27 14:22:22
问题 TimeSpan.FromSeconds takes a double, and can represent values down to 100 nanoseconds, however this method inexplicably rounds the time to whole milliseconds. Given that I've just spent half an hour to pinpoint this (documented!) behaviour, knowing why this might be the case would make it easier to put up with the wasted time. Can anyone suggest why this seemingly counter-productive behaviour is implemented? TimeSpan.FromSeconds(0.12345678).TotalSeconds // 0.123 TimeSpan.FromTicks((long)

Can you round a .NET TimeSpan object?

浪子不回头ぞ 提交于 2019-11-27 13:36:06
Can you round a .NET TimeSpan object? I have a Timespan value of: 00:00:00.6193789 Is there a simple way to keep it a TimeSpan object but round it to something like 00:00:00.62? Sorry, guys, but both the question and the popular answer so far are wrong :-) The question is wrong because Tyndall asks for a way to round but shows an example of truncation . Will Dean's answer is wrong because it also addresses truncation rather than rounding . (I suppose one could argue the answer is right for one of the two questions, but let's leave philosophy aside for the moment...) Here is a simple technique

Find average of collection of TimeSpans

蹲街弑〆低调 提交于 2019-11-27 12:39:35
问题 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? GetTimeSpanAverage(List<TimeSpan> sourceList) { TimeSpan total = default(TimeSpan); var sortedDates = sourceList.OrderBy(x => x); foreach (var dateTime in sortedDates) { total += dateTime; } return TimeSpan.FromMilliseconds(total.TotalMilliseconds