Compare current date with saved in database and return validation

2

I am developing a program where there is a 15-day trial period.

When the user registers an account in the application, it takes the internet date and registers the current date and end date of the test period, which is 15 days after the current date, in the database.

data_registro | data_vencimento
01/06/2017    | 16/06/2017

How do I compare the date provided by this code:

public static DateTime DataAtual()
{
    var myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
    var response = myHttpWebRequest.GetResponse();
    string todaysDates = response.Headers["date"];
    return DateTime.ParseExact(todaysDates,
                               "ddd, dd MMM yyyy HH:mm:ss 'GMT'",
                                CultureInfo.InvariantCulture.DateTimeFormat,
                                DateTimeStyles.AssumeUniversal);
}

The code above is returned by this code: DataAtual().ToShortDateString(); which results in: 06/01/2017

With the database, in the field: data_vencimento to validate whether or not the test period has expired?

    
asked by anonymous 01.06.2017 / 18:11

2 answers

3

Converts the dateLimite field to the DateTime type

   DateTime dt = Convert.ToDateTime(dataStringDoBanco); 

   DateTime dataAtual = DataAtual();


    if(dataAtual > dt)
    {
        // O tempo de testes expirou
    }
    
01.06.2017 / 18:24
4

Just compare one date with the other, the same is done with integers.

DateTime dataAtual = DataAtual();
DateTime dataLimite = /* Capturar a data do banco */;

if(dataAtual > dataLimite)
{
    // O tempo de testes expirou
}

As the field in the bank is a string (should be DateTime , right?) you will need to convert it to DateTime .

Since the date format will always be the same, you can use ParseExact

dataLimite = DateTime.ParseExact(dateString, "dd/MM/yyyy", CultureInfo.InvariantCulture);
    
01.06.2017 / 18:18