timespan

Format A TimeSpan With Years

北慕城南 提交于 2019-11-26 12:16:23
问题 I have a class with 2 date properties: FirstDay and LastDay . LastDay is nullable. I would like to generate a string in the format of \"x year(s) y day(s)\" . If the total years are less than 1, I would like to omit the year section. If the total days are less than 1, I would like to omit the day section. If either years or days are 0, they should say \"day/year\", rather than \"days/years\" respectively. Examples: 2.2 years: \"2 years 73 days\" 1.002738 years: \"1 year 1 day\" 0.2 years: \

Showing Difference between two datetime values in hours

空扰寡人 提交于 2019-11-26 12:08:21
I am retrieving two date time values from the database. Once the value is retrieved, I need the difference between the two values. For that, I create a timespan variable to store the difference of the 2 date values. TimeSpan? variable = datevalue1 - datevalue2; Now i need to show the difference which is stored in the Timespan variable in terms of number of hours. I referred to TimeSpan.TotalHours but couldn't apply the same for some reason. How do I do that? I am using C# on a MVC project. I simple need to show the difference value in hours? EDIT: Since timespan was nullable, i couldn't use

calculating the difference in months between two dates

被刻印的时光 ゝ 提交于 2019-11-26 12:07:21
In C#/.NET TimeSpan has TotalDays , TotalMinutes , etc. but I can't figure out a formula for total months difference. Variable days per month and leap years keep throwing me off. How can I get TotalMonths ? Edit Sorry for not being more clear: I know I can't actually get this from TimeSpan but I thought using TotalDays and TotalMinutes would be a good example to express what I was looking for ... except I'm trying to get Total Months. Example: Dec 25, 2009 - Oct 6, 2009 = 2 TotalMonths. Oct 6th to Nov 5th equals 0 months. On Nov 6th, 1 month. On Dec 6th, 2 months You won't be able to get that

Do we have a TimeSpan sort of class in Java

烂漫一生 提交于 2019-11-26 11:19:51
问题 I was just wondering if there is a need of TimeSpan in java.util so that I can define how much hours,minutes and seconds are there in between these two times. From this TimeSpan we can have a time interval between two times. like TimeSpan getTimeSpan( Date before, Date after ){...} or long timeSpan = System.currentTimeMillis(); // ... long job timeSpan = System.currentTimeMillis() - timeSpan; TimeSpan ts = new TimeSpan(timeSpan); and with this TimeSpan we can use it in SimpleDateFormat .

Check if datetime instance falls in between other two datetime objects

眉间皱痕 提交于 2019-11-26 11:05:27
问题 I would like to know a simple algorithm to check if the given instance of datetime lies between another two instances in C# . Note: I skimmed though this How do I check if a given datetime object is "between" two datetimes? and it was for python and many more for php. Most of the other questions were regarding difference between the two. Details: I am more specific about the time, date does not matter to me. For example i got DataBase entry for a staff who works between 10:00 Am - 9:00 Pm and

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

蓝咒 提交于 2019-11-26 09:54:11
问题 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. 回答1: 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(':')

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

ⅰ亾dé卋堺 提交于 2019-11-26 08:21:25
问题 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? 回答1: You can work around this very easily by changing your signature. void Foo(TimeSpan? span = null) { if (span == null) { span = TimeSpan.FromSeconds(2);

How to Convert string “07:35” (HH:MM) to TimeSpan

半腔热情 提交于 2019-11-26 08:16:05
问题 I would like to know if there is a way to convert a 24 Hour time formatted string to a TimeSpan. Right now I have a \"old fashion style\": string stringTime = \"07:35\"; string[] values = stringTime.Split(\':\'); TimeSpan ts = new TimeSpan(values[0], values[1], 0); 回答1: While correct that this will work: TimeSpan time = TimeSpan.Parse("07:35"); And if you are using it for validation... TimeSpan time; if (!TimeSpan.TryParse("07:35", out time)) { // handle validation error } Consider that

What is the correct SQL type to store a .Net Timespan with values > 24:00:00?

≯℡__Kan透↙ 提交于 2019-11-26 08:03:51
I am trying to store a .Net TimeSpan in SQL server 2008 R2. EF Code First seems to be suggesting it should be stored as a Time(7) in SQL. However TimeSpan in .Net can handle longer periods than 24 hours. What is the best way to handle storing .Net TimeSpan in SQL server? I'd store it in the database as a BIGINT and I'd store the number of ticks (eg. TimeSpan.Ticks property). That way, if I wanted to get a TimeSpan object when I retrieve it, I could just do TimeSpan.FromTicks(value) which would be easy. Thanks for the advice. As there is no equivalent in SQL server. I simply created a 2nd field

C#: what is the easiest way to subtract time?

拥有回忆 提交于 2019-11-26 05:34:13
问题 I\'m trying to put together a tool that will help me make work schedules. What is the easiest way to solve the following? 8:00am + 5 hours = 1:00pm and 5:00pm - 2 hours = 3:00pm and 5:30pm - :45 = 4:45 and so on. 回答1: These can all be done with DateTime.Add(TimeSpan) since it supports positive and negative timespans. DateTime original = new DateTime(year, month, day, 8, 0, 0); DateTime updated = original.Add(new TimeSpan(5,0,0)); DateTime original = new DateTime(year, month, day, 17, 0, 0);