timespan

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

旧街凉风 提交于 2019-11-27 03:17:53
问题 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,

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

て烟熏妆下的殇ゞ 提交于 2019-11-27 03:02:33
问题 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" 回答1: 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

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

折月煮酒 提交于 2019-11-27 03:01:38
问题 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. 回答1: Do you want calculate the age in years for an employee? Then you can use this snippet (from Calculate age in C#):

Why does TimeSpan.ToString() require escaping separators?

橙三吉。 提交于 2019-11-27 02:41:44
问题 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

TimeSpan to DateTime conversion

£可爱£侵袭症+ 提交于 2019-11-27 02:28:56
问题 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? 回答1: 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

How to parse string with hours greater than 24 to TimeSpan?

大憨熊 提交于 2019-11-27 01:57:31
How to parse string like 30:15 to TimeSpan in C#? 30:15 means 30 hours and 15 minutes. string span = "30:15"; TimeSpan ts = TimeSpan.FromHours( Convert.ToDouble(span.Split(':')[0])). Add(TimeSpan.FromMinutes( Convert.ToDouble((span.Split(':')[1])))); This does not seem too elegant. LukeH If you're certain that the format will always be "HH:mm" then try something like this: string span = "35:15"; TimeSpan ts = new TimeSpan(int.Parse(span.Split(':')[0]), // hours int.Parse(span.Split(':')[1]), // minutes 0); // seconds I'm using a simple method that I devised a long time ago and just posted

Why does TimeSpan.ParseExact not work

笑着哭i 提交于 2019-11-27 01:57:02
This is a bit wierd. Parsing a text field with a valid timespan fails if I try to be precise! const string tmp = "17:23:24"; //works var t1 = TimeSpan.Parse(tmp); //fails var t2 = TimeSpan.ParseExact(tmp, "hh:mm:ss", System.Globalization.CultureInfo.InvariantCulture); The second parse fails with an exception "Input string was not in a correct format." from DateTime. From the documentation : Any other unescaped character in a format string, including a white-space character, is interpreted as a custom format specifier. In most cases, the presence of any other unescaped character results in a

C# 4.0: Can I use a TimeSpan as an optional parameter with a default value?

纵然是瞬间 提交于 2019-11-26 22:31:12
Both of these generate an error saying they must be a compile-time constant: void Foo(TimeSpan span = TimeSpan.FromSeconds(2.0)) void Foo(TimeSpan span = new TimeSpan(2000)) First of all, can someone explain why these values can't be determined at compile time? And is there a way to specify a default value for an optional TimeSpan object? You can work around this very easily by changing your signature. void Foo(TimeSpan? span = null) { if (span == null) { span = TimeSpan.FromSeconds(2); } ... } I should elaborate - the reason those expressions in your example are not compile-time constants is

How to serialize a TimeSpan to XML

泪湿孤枕 提交于 2019-11-26 18:46:19
问题 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

Check if a given time lies between two times regardless of date

倾然丶 夕夏残阳落幕 提交于 2019-11-26 18:45:49
I have timespans: String time1 = 01:00:00 String time2 = 05:00:00 I want to check if time1 and time2 both lies between 20:11:13 and 14:49:00 . Actually, 01:00:00 is greater than 20:11:13 and less than 14:49:00 considering 20:11:13 is always less than 14:49:00 . This is given prerequisite. So what I want is, 20:11:13 < 01:00:00 < 14:49:00 . So I need something like that: public void getTimeSpans() { boolean firstTime = false, secondTime = false; if(time1 > "20:11:13" && time1 < "14:49:00") { firstTime = true; } if(time2 > "20:11:13" && time2 < "14:49:00") { secondTime = true; } } I know that