Operations with time quantities and conversion between time units

Asked

Viewed 2,292 times

3

Is there a library specializing in the handling of quantities and units of time?
Allowing the conversion of/into milliseconds, seconds, minutes and time sum and subtraction operations.

Native in c# I found nothing that does not require much effort and conversion calculations.

Or if it exists natively how could it obtain the total of hours by adding two amounts of time, for example:

03:20 + 03:50 = ?

There is a way to do this without having to untangle the hours and minutes, making validations for when the minutes pass 59 become an hour more?

  • Yes, there is FCL https://en.wikipedia.org/wiki/Standard_Libraries_(CLI)

  • Give me an example of how I do the sum of hours with FCL please. for example add 00:59 + 00:40

  • 2

    Hours cannot be summed up, this makes no sense, hours are points in time.

  • 3

    @Meajudasilvio I don’t know if I got it right, but it would be something like that?

  • 1

    @Randrade is exactly that, thank you very much, can by as an answer, and if possible give an explanation to our friend of how it is possible to add "two points in time"

  • 1

    @Meajudasilvio Your question is currently closed and cannot be answered. You can [Dit] the question explaining the best for it to be reopened. It would be useful for you to add a clearer example question, such as "How to get the total hours by adding two hours, for example:03:20 + 03:50? I think it would make it clearer and it could be reopened.

  • @Meajudasilvio See if this example helps you: https://dotnetfiddle.net/YQkmqF

  • 1

    You asked for the library and I showed you, if you didn’t want to know that, don’t ask. https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110). aspx This is the timekeeping object. Look at the summation methods and look for one that adds up to two times. It does not. Do you think Microsoft forgot to put it? She didn’t put it because it doesn’t make sense. Shows me an operation where you need to add an input and an output point and what the expected result is. This does not exist.

  • 1

    http://infiniteundo.com/post/25326999628/falsehoods-programmers-believe-abouttime, http://infiniteundo.com/post/25509354022/more-falsehoods-programmers-believe-abouttime (has more than 100 things that most people think they know about and don’t. https://en.wikipedia.org/wiki/Time If you improve your question as everyone has indicated it may be reopened, complaining is not possible. Inform what you really want, add two hours is not possible.

  • 2

    What @bigown said about not being able to "add hours" makes sense, if you remember that hours are not integer numerical values, you don’t apply arithmetic operations directly over hours. You need to convert to arrive at something like "sum of hours". Perhaps the best way to do this simply is to adopt the same pattern used in Unix: convert the hours into seconds, do the operations (sum, subtraction, etc...) with the seconds and then convert them back. This way you ensure the result and it is not difficult to implement. (Something simple, of course.)

  • Blz. Thank you..

  • 6
  • 3

    @Meajudasilvio If the edition I made does not match what I intended to ask, please reverse it or say that I do.

  • In my view it has become much more coherent, thank you very much for your help.

Show 9 more comments

2 answers

19


You can add the hours normally in C#. For this, just ensure that the time is one time interval.

You can perform operations, without problems, with formats like Timespan and Datetime (for dates, together with times).

To sum up two times, or more, just add normally. See the example below:

var time = new TimeSpan(3, 20, 0);
var time2 = new TimeSpan(3, 50, 0);

var total = time + time2;
Console.WriteLine(total);
//Resultado: 07:10:00

Example in . NET Fiddle.

It is worth mentioning that you have several methods for use with Timespan. With them, you’ll get more accurate results.

An example is if you add values that exceed 24 hours. See the example below:

var time = new TimeSpan(20, 20, 0);
var time2 = new TimeSpan(13, 50, 0);

var total = time.Add(time2);
Console.WriteLine(total.TotalHours);
//Resultado: 34.1666666666667

Example in . NET Fiddle.

See the image below to better understand the values you have without having to "perform validations" (as you said in your question):

Print

If you want to see more ways to do this, see a few more examples in our older brother.

  • Excellent answer thank you.

  • 1

    I decided not to deny it yet, but the first sentence is already wrong. The codes shown do not add up to hours. The question so far does not say what it really wants to add up. If it is hours, it does not give and this answer is wrong. It teaches wrong. Now if the question is formulated with the information necessary to know what the AP wants, then it may be that it is almost all right. Of course, him accepting it might look like that. But we have no way of knowing, because there is another possible answer quite different from this depending on what the question actually is. He may have been content and learned wrong.

  • 6

    @bigown You participated in the comments (it was even the first). I’m sure that with them you’ve really managed to understand what the AP wants. If you’re bothered that I used the term "hours," I added a reference to the "time interval", which is used by MSDN. Now, about being wrong, it would be interesting for you to show the reason.+

  • 1

    In the links in your comment do not say that but many fallacies about time. Now, whether you want to negatively or not, I can no longer, and do not want to, do anything. I’m sure the answer helped the AP and now he’ll know sum up a working day if necessary.

  • When I have time I will make a canonical on the subject because almost nobody understands about it. And the question does not say what you want, several different answers can be posted. In the current form you can not answer.

  • 5

    An extra detail for the answer that might be interesting would be this TimeSpan parsed = TimeSpan.Parse("23:30"); or DateTime parsed = DateTime.Parse("23:30");

Show 1 more comment

6

It is very quiet to manipulate dates and times if they are in Datetime format

//Cria uma nova data 09:00:00 06/06/2017
DateTime teste = new DateTime(2017, 6, 6, 9, 0, 0);
//Cria uma nova data 12:00:00 06/06/2017
DateTime teste2 = new DateTime(2017, 6, 6, 12, 0, 0);

//Adiciona uma hora, o resultado será 10:00:00 06/06/2017
teste.AddHours(1);
//Adiciona 30 minutos, o resultado será 09:30:00 06/06/2017
teste.AddMinutes(30)
//Soma a segunda data a primeira, o resultado será 21:00:00 06/06/2017
teste.Add(teste2.TimeOfDay());
  • 1

    This isn’t adding up hours in the way AP described it. It’s even adding up in a way that obviously works, but it’s a time with a number of hours, they’re different concepts.

  • 2

    The idea was to demonstrate the methods of manipulating the Datetime class, and I really forgot to put the most important in his case

  • 1

    Dear thanks for the answer, I accepted Randrade’s because in his case, he offers me an option where I accumulate the hours and not just navigating in time, and I needed exactly this one way to add and accumulate the time. (hours:minutes)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.