Doing operations/handling Datetime

Asked

Viewed 1,191 times

3

The idea is for the user to type in a textbox to horaEntrada and in another to horaSaida where in the case of these two variables they should have the format "HH:mm" hours and minutes only thereafter by the method get and set there would assign the variables I mentioned above, after that I created the method TempoPermanencia in which the intention is to subtract the hours and minutes of these variables in order to be able to display the time that the person was parked on the computer screen

Follows the code:

    private string placa, modelo;
    private DateTime horaEntrada, horaSaida;

    public string Placa
    {
        get { return placa; }
        set { placa = value; }
    }

    public string Modelo
    {
        get { return modelo; }
        set { modelo = value; }
    }

    public DateTime HoraEntrada
    {
        get { return horaEntrada; }
        set { horaEntrada = value; }
    }

    public DateTime HoraSaida
    {
        get { return horaSaida; }
        set { horaSaida = value; }
    }

    public DateTime TempoPermanencia()
    {

    }
  • Explain better what you want. Set clear criteria. Should you take what? Do? What result do you want? Put where? Does this code indicate anything? What part of it is useful for the problem?

  • I will take time and timeSide and do the following: take the hours of timeSide and subtract with the hours of timeSbetween then the same thing with the minutes, so that I return another Datetime with the hours and minutes that the person was in place, put in the Tempopermanencia method, indicates yes, the time a person was parked, I would also like to know how to declare so that a Datetime only accept formats in hours and minutes, grateful.

  • It has already changed in relation to what is described in the question. DateTime does not serve for it. I will try to do something useful. How date and time is input? Are you sure you’ll discard the other information than time and minute?

  • Yes, just hour and minute

  • They are tickets yes sir tbm.

  • There won’t be no date

  • He’s getting more and more confused.

  • Let me explain it to you

  • The idea is for the user to type in a textbox the hourEntered and in another the hourAs the case where these 2 variables should have the format "HH:mm" hours and minutes, only, after that by the get and set method there would assign the variables that I mentioned above, after that I created the Temp method where the intention is to subtract the hours and minutes of these variables in order to be able to display the time that the person was parked on the computer screen, understood ?

  • It would be better to [Dit] the question and consolidate all this in a unique way. I’m making an answer, but some points I don’t know how to resolve because I don’t have the requirement.

Show 5 more comments

2 answers

2

I understand what you want.

If you do HoraEntrada - HoraSaida the return will be a TimeSpan

The fields HoraEntrada and HoraSaida may be DateTime, makes sense, because the car enters/leaves in a day and time

You must change your on-call time

public TimeSpan TempoPermanencia()
{
    return HoraSaida - HoraEntrada;
}

Can also be

return HoraSaida.Substract(HoraEntrada);

With the object TimeSpanyou can format with hh:mm, also have TotalDays, TotalHours, etc.

To show off would be something like this:

TempoPermanencia().ToString(@"hh\:mm\:ss")
  • Tostring went wrong, it seems that the \

  • something was missing. I edited. It lacked a @

  • Perfect friend of mine...

  • @ is for something specific ?

  • Server to "escape" the string. The backslash should be used like: n or r for example. If you want it in the literal you must use @ before "opening" the string

2


To calculate the difference just make a subtraction, like this:

public TimeSpan TempoPermanencia() => HoraSaida - HoraEntrada;

This will produce a TimeSpan which is exactly what you need. DateTime, Contrary to what many people think is just a point in time, nothing else, so an input or output moment is a point in time. A period, an amount of time spent on something is represented by a TimeSpan. Read his documentation to learn how to use it, take the stored time of the various possible.

The ideal would be not to consider only the hour and the minute. Can’t it be that the exit occurs the next day? Is there not the slightest chance of this occurring? Does it make such a difference to disregard seconds? If seconds are not important, wouldn’t it be interesting if they were discarded in the data entry? It would even be possible to do this on the property, but it would have to use clear criteria. Without certain requirements, no code will be produced right.

I made a more "complete" solution that I do not guarantee that does exactly what you want, lacking criteria to solve certain situations, for example, in the test done can give 1 hour and 43 minutes, or 1 hour and 44 minutes, It works, but there is no clear criterion as to whether this is what should happen. I didn’t even test it. Messing with date and time is a lot more complicated than it looks. I cut out the superfluous information on the property, but I could have stored it all and made that cut account only at the time of calculation.

using System;
using static System.Console;
                    
public class Program {
    public static void Main() {
        var objeto = new AlgumaClasse();
        objeto.HoraEntrada = DateTime.Now;
        objeto.HoraSaida = DateTime.Now.AddHours(1).AddMinutes(43).AddSeconds(22);
        WriteLine($"Permaneceu {(objeto.TempoPermanencia().ToString(@"hh\:mm"))}");
    }
}

class AlgumaClasse {
    public string Placa { get; set; }
 
    public string Modelo { get; set; }

    private DateTime horaEntrada;
    public DateTime HoraEntrada {
        get => horaEntrada;
        set {
            var tempo = default(DateTime).Add(value.TimeOfDay); //corta data
            horaEntrada = tempo.AddSeconds(-tempo.Second); //corta segundos
        }
    }

    private DateTime horaSaida;
    public DateTime HoraSaida {
        get=> horaSaida;
        set {
            var tempo = default(DateTime).Add(value.TimeOfDay);
            horaSaida = tempo.AddSeconds(-tempo.Second);
        }
    }
    public TimeSpan TempoPermanencia() => HoraSaida - HoraEntrada;
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

  • It’s just an exercise in my course that specifically asks for only hours and minutes, but you’re right, thanks for the help

  • Well complete @bigown, but the guy will have to explain this to the teacher, I think he’s starting. But really good as always his answers

  • @Murilo is nowhere near ideal, there are several things that do not work well in "real life". Either the given requirement is too complex for him, or he’ll manage with what was posted here. Anyway, whenever the person does not understand something can ask a new question. Thank you.

  • That’s it! I didn’t give you +1 yet I’ve finished my quota of the day.

  • @Murilo but I give in his :P

  • @Bigown valeu master, I don’t need to explain no, it’s a review for the test and probably the teacher shouldn’t even look, but I like to learn a thing or two mainly here, because you who answer more often have very complete answers, thing that in the microsoft manual gets a little confused, what is worth it is the learning, after I even made an operation joining double with the Timespan, in the leaps and ravines we learn ;)

Show 1 more comment

Browser other questions tagged

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