Datetime object with DIA, MES, HORA

Asked

Viewed 260 times

1

I need to create an object of the type Datetime that contains only Day, Month and Time. You may have minutes and seconds but you can’t have a year.

How do I do that?

1 answer

2


Well, you can create your own type - but a Datetime always has full date and time. You can always ignore the year - or use the current year:

DateTime data = new DateTime(DateTime.Now.Year, mes, dia);

To create your own type you can do something similar to this:

  public struct MesDia : IEquatable<MesDia>
{
    private readonly DateTime data;

    public MesDia(int mes, int dia)
    {
        data = new DateTime(2018, mes, dia);
    }

    public MesDia AddDays(int dia)
    {
        DateTime added = data.AddDays(dia);
        return new MesDia(added.Month, added.Day);
    }

    public bool Equals(MesDia other)
    {
       //implementar;
    }
}

Browser other questions tagged

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