timespan

Parse exact custom time format?

喜欢而已 提交于 2019-11-28 10:32:22
问题 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? 回答1: I'm not aware of the ability to specify optional parts like that in a custom format string. I suggest you

How do I format a timespan to show me total hours?

有些话、适合烂在心里 提交于 2019-11-28 09:59:28
I want to save the user's hours worked in a database varchar column, but by default, the formatted value includes days if the number of hours is more than 24. I just want the total number of hours. For example: if a user works 10:00:00 hours today, then 13:00:00 hours tomorrow, and 3:30:00 hours the day after tomorrow then the formatted total I want is 26:30:00. Instead, I am seeing 1.2:30:00. How can I get the formatting I want? Also, when I save the value 40:00:00 in the database manually, and try to read it into a TimeSpan later, I get a bug. How can I save the hours in the database the way

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

半腔热情 提交于 2019-11-28 09:57:42
问题 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

Why isn't my TimeSpan.Add() working?

情到浓时终转凉″ 提交于 2019-11-28 09:41:00
There has to be an easy answer: var totalTime = TimeSpan.Zero; foreach (var timesheet in timeSheets) { //assume "time" is a correct, positive TimeSpan var time = timesheet.EndTime - timesheet.StartTime; totalTime.Add(time); } There's only one value in the list timeSheets and it is a positive TimeSpan (verified on local inspection). TimeSpans are value types. Try: totalTime = totalTime.Add(time) This is a common mistake. TimeSpan.Add returns a new instance of TimeSpan . Jason Coyne totalTime = totalTime.Add(time) 来源: https://stackoverflow.com/questions/3580947/why-isnt-my-timespan-add-working

Convert TimeSpan from format “hh:mm:ss” to “hh:mm”

霸气de小男生 提交于 2019-11-28 09:36:21
I want to show in a TextBox only hour and minutes var test = dataRow.Field<TimeSpan>("fstart").ToString(); //test ="08:00:00" var tb = (TextBox) gridViewRow.Cells[2].FindControl("fstart"); tb.Text = test; how to show only hours and minutes "hh.mm" You need to convert your data to TimeSpan and then use format: "hh\:mm" string test ="08:00:00"; TimeSpan ts = TimeSpan.Parse(test); Console.Write(ts.ToString(@"hh\:mm")); In your case: var test = dataRow.Field<TimeSpan>("fstart").ToString(@"hh\:mm")); Remember to escape the colon : You may see: Custom TimeSpan Format Strings There is no need to

Calculate the difference between two dates and get the value in years? [duplicate]

穿精又带淫゛_ 提交于 2019-11-28 09:35:18
Possible Duplicate: How do I calculate someone’s age in C#? I want to calculate basically the age of employees - So we have DOB for each employee, So on the C# Side I want to do something like this - int age=Convert.Int32(DateTime.Now-DOB); I can use days and manipulate then get the age...but I wanted to know if there something I can use directly to get the number of years. alexn Do you want calculate the age in years for an employee? Then you can use this snippet (from Calculate age in C# ): DateTime now = DateTime.Today; int age = now.Year - bday.Year; if (bday > now.AddYears(-age)) age--;

Why does TimeSpan.ToString() require escaping separators?

╄→尐↘猪︶ㄣ 提交于 2019-11-28 09:12:29
You can specify a custom format for a DateTime object like this: DateTime.Now.ToString("HH:mm:ss"); // 19:55:23 But when I try to use the same format for a TimeSpan object like this: DateTime.Now.TimeOfDay.ToString("HH:mm:ss"); I get the "Input string was not in a correct format." exception. It turns out, the solution is that you need to escape the ':' characters like in "HH\\:mm\\:ss" . Note that there is a double backslash because if you specify only one, it will break the string so you need to escape that one too. The question is, why .NET Framework developers made it this way? There must

TimeSpan to DateTime conversion

▼魔方 西西 提交于 2019-11-28 08:51:39
I want to convert a Timespan to Datetime. How can I do this? I found one method on Google: DateTime dt; TimeSpan ts="XXX"; //We can covnert 'ts' to 'dt' like this: dt= Convert.ToDateTime(ts.ToString()); Is there any other way to do this? Arif Eqbal It is not very logical to convert TimeSpan to DateTime. Try to understand what leppie said above. TimeSpan is a duration say 6 Days 5 Hours 40 minutes. It is not a Date. If I say 6 Days; Can you deduce a Date from it? The answer is NO unless you have a REFERENCE Date. So if you want to convert TimeSpan to DateTime you need a reference date. 6 Days &

Timespan intersection in c#

旧街凉风 提交于 2019-11-28 07:30:32
问题 Let's say i have 2 date ranges. Those date ranges could be represented as time spans. i want to find a date range, that falls within the two time spans. Range 1: 2/1/2011 - 8/1/2011 (timespan of 6 months) Range 2: 5/2/2011 - 5/28/2011 (timespan of 26 days) so in this case, the intersection would be 5/2/2011-5/28/2011, but the ranges could move in either direction, (or not intersect at all in which case i'd want the resulting timespan to be 0 length) in the end, i need the calendar dates of

Using DateTime?.Value.TimeOfDay in LINQ Query

痞子三分冷 提交于 2019-11-28 07:24:13
问题 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