timespan

How do I convert ticks to minutes?

十年热恋 提交于 2019-11-29 22:45:27
I have a ticks value of 28000000000 which should be 480 minutes but how can I be sure? How do I convert a ticks value to minutes? TimeSpan.FromTicks(28000000000).TotalMinutes; A single tick represents one hundred nanoseconds or one ten-millionth of a second. FROM MSDN. So 28 000 000 000 * 1/10 000 000 = 2 800 sec. 2 800 sec /60 = 46.6666min Or you can do it programmaticly with TimeSpan: static void Main() { TimeSpan ts = TimeSpan.FromTicks(28000000000); double minutesFromTs = ts.TotalMinutes; Console.WriteLine(minutesFromTs); Console.Read(); } Both give me 46 min and not 480 min...

Parse exact custom time format?

时间秒杀一切 提交于 2019-11-29 16:27:21
I can't seem to get the custom format right for my TimeSpan.ParseExact() : Time samples to be expected: 1:23:45.6 23:45.6 23:45 1:23:45 The string format I'm using: string withTenthFormat = @"[%h\:]mm\:ss[\.%f]"; Trying to have hours and fractions of seconds optionally. However using this format and an CultureInfo.InvariantCulture does lead to a FormatException . What am I missing? I'm not aware of the ability to specify optional parts like that in a custom format string. I suggest you use multiple format strings, and use the overload of TimeSpan.ParseExact that takes an array of formats.

'TimeOfDay' is not supported in LINQ to Entities - Find total seconds in Linq 2 SQL

不问归期 提交于 2019-11-29 15:46:12
When I do the following : [EdmFunction("Edm", "TruncateTime")] public static DateTime? TruncateDateTime(DateTime? inputDate) { return inputDate.HasValue ? inputDate.Value.Date : (DateTime?)null; } [EdmFunction("Edm", "TotalSeconds")] public static double? GetTotalSeconds(DateTime? inputDate) { return inputDate.HasValue ? inputDate.Value.TimeOfDay.TotalSeconds: (double?)null; } var employeeSelection = (from c in MyContext.Employees.Where(c => c.From.HasValue && c.To.TimeOfDay.TotalSeconds != 0) ... select new Employee { To = c.To != DateTime.MinValue && c.To.TimeOfDay.TotalSeconds != 0 ?

Using DateTime?.Value.TimeOfDay in LINQ Query

亡梦爱人 提交于 2019-11-29 13:32:22
I'm trying to do a Query with LINQ on ASP.NET MVC 3. I have a model, lets call it Event. This Event object has a Date property, of DateTime?. What I want is to fetch the Events that are between 2 TimeSpans. Right now my code looks like the following: TimeSpan From = new TimeSpan(8,0,0); TimeSpan Until = new TimeSpan(22,0,0); var events = from e in db.Events where e.Date.Value.TimeOfDay >= From, e.Date.Value.TimeOfDay <= Until select e; An exception is thrown, telling me that "The specified type member 'TimeOfDay' is not supported in LINQ to Entities." I don't get a way around this problem, and

AM/PM to TimeSpan

天大地大妈咪最大 提交于 2019-11-29 09:09:34
I want to achieve the converse of this , that is, I want to convert a string with format hh:mm tt to a TimeSpan with zeroed seconds. For example, 09:45 pm is converted to 21:45:00 . The simplest approach would probably be to parse it as a DateTime using DateTime.ParseExact , and then use the TimeOfDay to exact the TimeSpan . DateTime dateTime = DateTime.ParseExact(text, "hh:mm tt", CultureInfo.InvariantCulture); TimeSpan span = dateTime.TimeOfDay; It's odd to see a leading 0 on a number of hours when you're also specifying an am/pm designator though. You might want "h" instead of "hh" in the

Format TimeSpan in DataGridView column

﹥>﹥吖頭↗ 提交于 2019-11-29 07:08:56
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. I just discovered this myself. Unfortunately, the solution is pretty involved. The good news is that it works. Firstly, you need an ICustomFormatter

How to Convert ISO 8601 Duration to TimeSpan in VB.Net?

僤鯓⒐⒋嵵緔 提交于 2019-11-29 06:36:07
问题 Is there a standard library method that converts a string that has duration in the standard ISO 8601 Duration (also used in XSD for its duration type) format into the .NET TimeSpan object? For example, P0DT1H0M0S which represents a duration of one hour, is converted into New TimeSpan(0,1,0,0,0). A Reverse converter does exist which works as follows: Xml.XmlConvert.ToString(New TimeSpan(0,1,0,0,0)) The above expression will return P0DT1H0M0S. 回答1: This will convert from xs:duration to TimeSpan

TimeSpan FromMilliseconds strange implementation?

守給你的承諾、 提交于 2019-11-29 05:27:22
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 Microsoft design the FromMilliseconds method to take a double a parameter instead of a long (since a double

Parse string to TimeSpan

只愿长相守 提交于 2019-11-29 05:26:45
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? 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()); John Sheehan DateTime.ParseExact or DateTime.TryParseExact lets you specify the exact format of the input. After you get the DateTime , you can grab the DateTime.TimeOfDay which is a TimeSpan . In the absence of TimeSpan.TryParseExact , I think an 'elegant' solution

How do I convert a 12 hour time string into a C# TimeSpan?

…衆ロ難τιáo~ 提交于 2019-11-29 03:38:46
When a user fills out a form, they use a dropdown to denote what time they would like to schedule the test for. This drop down contains of all times of the day in 15 minute increments in the 12 hour AM/PM form. So for example, if the user selects 4:15 pm, the server sends the string "4:15 PM" to the webserver with the form submittion. I need to some how convert this string into a Timespan, so I can store it in my database's time field (with linq to sql). Anyone know of a good way to convert an AM/PM time string into a timespan? Phil Lamb You probably want to use a DateTime instead of TimeSpan