timespan

Formatting MVC model TimeSpan field

有些话、适合烂在心里 提交于 2019-11-29 02:27:13
问题 I have a MVC page containing a few timepickers. These are stored in a list of objects in the model as nullable TimeSpan . The problem is that I get the timespans printed with seconds into the input fields. Is there some way to format these model properties so that I get the timespans printed in some other way (like 7:00 , instead of 07:00:00 )? Needless to say, my "suggestion" of [DisplayFormat...] didn't work. At least not the way I hoped. The input fields are defined like this: @Html

TimeSpan.Parse time format hhmmss

天大地大妈咪最大 提交于 2019-11-29 01:32:24
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? 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.TimeOfDay.TotalSeconds); Console.ReadLine(); } } } Note this will not handle 24HR times, to parse times in 24HR

Is TimeSpan unnecessary?

怎甘沉沦 提交于 2019-11-28 23:18:44
EDIT 2009-Nov-04 OK, so it's been a little while since I first posted this question. It seems to me that many of the initial responders failed to really get what I was saying--a common response was some variation on "What you're saying doesn't make any sense"--and so I've made some handy diagrams to really illustrate my point. When we speak of numbers, we are generally referring to points on what grade school children learn is called the Number Line: Now, when we learn arithmetic, our minds learn to perform a very interesting transformation of this concept. Evalutating the expression 1 + 0.5 ,

How to produce “human readable” strings to represent a TimeSpan

本秂侑毒 提交于 2019-11-28 23:17:49
I have a TimeSpan representing the amount of time a client has been connected to my server. I want to display that TimeSpan to the user. But I don't want to be overly verbose to displaying that information (ex: 2hr 3min 32.2345sec = too detailed!) For example: If the connection time is... > 0 seconds and < 1 minute -----> 0 Seconds > 1 minute and < 1 hour -----> 0 Minutes, 0 Seconds > 1 hour and < 1 day -----> 0 Hours, 0 Minutes > 1 day -----> 0 Days, 0 Hours And of course, in cases where the numeral is 1 (ex: 1 seconds, 1 minutes, 1 hours, 1 days), I would like to make the text singular (ex:

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

爷,独闯天下 提交于 2019-11-28 22:45:25
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? 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 Bubba Store as a varchar. Save to sql using TimeSpan.ToString() . Read from sql as: TimeSpanObj = TimeSpan.Parse

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

隐身守侯 提交于 2019-11-28 22:31:14
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)(TimeSpan.TicksPerSecond * 0.12345678)).TotalSeconds // 0.1234567 As you've found out yourself, it's a

Find average of collection of TimeSpans

a 夏天 提交于 2019-11-28 20:03:28
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/sortedDates.Count()); } vc 74 You can use the Average extension method : double doubleAverageTicks =

subtract 2 datetime fields to get the days left difference

让人想犯罪 __ 提交于 2019-11-28 19:51:33
问题 Would appreciate it if anyone can help me figure out to substract 2 datetime fields to get the days left difference. 回答1: This is very easy to do with C#. For comparing DateTimes, we have a class called TimeSpan . The TimeSpan structure, in this case, would be defined as the difference between your two datetimes . Let's say that your DateTimes are called start and end. DateTime start = new DateTime(2009, 6, 14); DateTime end = new DateTime(2009, 12, 14); We have established our DateTimes to

What are timer ticks, the unit used by Stopwatch.ElapsedTicks

冷暖自知 提交于 2019-11-28 13:23:10
I used to think that Stopwatch.ElapsedTicks was equal to Stopwatch.Elapsed.Ticks . But it isn't. While the latter is a number of 100 nanoseconds periods, the former use a mysterious unit called timer ticks . I wonder what those are, and why are they different from the usual unit of measurement? From the docs : The Stopwatch measures elapsed time by counting timer ticks in the underlying timer mechanism. If the installed hardware and operating system support a high-resolution performance counter, then the Stopwatch class uses that counter to measure elapsed time. Otherwise, the Stopwatch class

JavaScriptSerializer not deserializing DateTime/TimeSpan Properly

佐手、 提交于 2019-11-28 13:08:36
Having a problem where DateTime/TimeSpan doesn't seem to deserialize properly with JavaScriptSerializer. When I get the Object back after deserializing the TimeSpan is empty and if I use DateTime then the times are all out of whack. Did find this article but it didn't really help me too much. http://www.west-wind.com/weblog/ShowPost.aspx?id=471402 Anyone have any ideas? Should I maybe try the json.net library? public class JsonFilter : ActionFilterAttribute { public string Param { get; set; } public Type JsonDataType { get; set; } public override void OnActionExecuting(ActionExecutingContext