Compare current date with saved in database and return validation

Asked

Viewed 567 times

2

I’m developing a program where there’s 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 the date of the end 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 return of the above code is done by this code: DataAtual().ToShortDateString(); resulting in: 01/06/2017

With the database, in the field: data_vencimento to validate whether or not the testing period is over?

2 answers

5

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
}

Like the field on the bench is a string (should be DateTime, right? ) you’ll need to convert it to DateTime.

Since the date format will always be the same, it is possible to use the ParseExact

dataLimite = DateTime.ParseExact(dateString, "dd/MM/yyyy", CultureInfo.InvariantCulture);
  • 2

    With string it works?

  • 1

    No. Nor would I have why.

  • 2

    But in the bank the date is in string

  • 5

    Then convert to DateTime.

3


Converts the dataLimite field to the Datetime type

   DateTime dt = Convert.ToDateTime(dataStringDoBanco); 

   DateTime dataAtual = DataAtual();


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

Browser other questions tagged

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