Check if day exists in the month

10

I need to check / validate if a (numeric) day exists in a given month.

For example: I have day 31 , and since it does not exist in the month of February the condition will fail.

I think a simple if will solve the problem, where I construct a date and check if it exists.

    
asked by anonymous 07.02.2014 / 16:47

5 answers

16

You can find out the last day of the month 'x':

public bool IsDiaValido(int dia, int mes, int ano)
{
    int ultimoDiaMes = DateTime.DaysInMonth(ano, mes);
    if(dia > ultimoDiaMes || dia < 1)
        return false;
    else
       return true;
}

This way you do not need to put a try-catch, avoiding an unnecessary error.

    
07.02.2014 / 16:57
4

I do not know if it's the best way, but the way I do it is:

try
{
    var test = Convert.ToDateTime("31/02/2014");
}
catch (FormatException)
{
    // Não existe a data
}
    
07.02.2014 / 16:54
4

A simple DateTime.TryParse or DateTime.Parse already says whether date proposed in string is a valid date already taking into account the different days of each month and leap years:

public bool isValidDate(string value) {
    DateTime result;
    return DateTime.TryParse(value, out result);
}

Overload:

public bool isValidDate(int dia, int mes, int ano) {
    try {
        return DateTime.Parse(dia + "/" + mes + "/" + ano) > DateTime.MinValue;
    } catch { }
    return false;
}

And other variants ...

A try / catch is required for anomalous cases, such as day = 0 or day 32 , which is possible or any exception that may occur. In the case of TryParse it is not necessary because try / catch is already built into the function.

Tests run successfully:

c.isValidDate(null);        // false
c.isValidDate("");          // false
c.isValidDate("       ");       // false

c.isValidDate("31/01/2014");    // true
c.isValidDate("28/02/2014");    // true
c.isValidDate("29/02/2012");    // true
c.isValidDate("29/02/2014");    // false
c.isValidDate("-29/02/2014");   // false
c.isValidDate("29/0/2014");     // false

c.isValidDate(31, 01, 2014);    // true
c.isValidDate(28, 02, 2014);    // true
c.isValidDate(29, 02, 2012);    // true
c.isValidDate(29, 02, 2014);    // false

c.isValidDate(-1, 02, 2014);    // false
c.isValidDate(31, 01, -1);      // false
c.isValidDate(15, 0, 2014);     // false
c.isValidDate(99, 99, 9999);    // false
c.isValidDate(01, 01, 0001);    // true
    
07.02.2014 / 16:54
3

I decided to respond not to devote a redundant form of code. I'm also giving overload option to a string with the date.

public bool EhDiaValido(int dia, int mes, int ano) {
    return dia >= DateTime.MinValue && dia <= DateTime.DaysInMonth(ano, mes);
}

public bool EhDiaValido(string data) {
    DateTime resultado;
    return DateTime.TryParse(data, out resultado);
}

In C # 7 the last method can be written as:

public bool EhDiaValido(string data) {
    return DateTime.TryParse(data, out var resultado);
}

I've placed it on GitHub for future reference.

    
02.06.2014 / 06:13
1

Simple, try creating the date and see if it fails:

DateTime dia;
try {
  dia = new DateTime(year: 2014, month: 02, day: 31);
} catch (ArgumentOutOfRangeException ex) {
  // A data não existe, tratar
}

Simple function:

static bool DataExiste(int ano, int mes, int dia) {
  try {
    new DateTime(ano, mes, dia);
    return true;
  } catch (ArgumentOutOfRangeException) { }

  return false;
}
    
07.02.2014 / 16:54