Home > OS >  How to set Date limit in days?
How to set Date limit in days?

Time:11-15

I have been trying to set a time limit for a certain form to not function past a specified date in a month, but I have been unsuccessful so far.

I am working with a low-code windows interface software that does the majority of the actual coding in the background but since it has some limitations I need to code this Date limit in myself.

The best thing I reached after looking around was this:

DateTime temp = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 15);

And I would add a rule in the program that the date shouldn't be higher than the above. But for some reason it doesn't work giving me an "; expected" error (in line 1 char 29).

CodePudding user response:

you can use the below code. you have to use AddDays method.

DateTime now = DateTime.Now;
DateTime pastTime = now.AddDays(-15);
DateTime futureTime = now.AddDays(15);

It will give you backdate if you provide a negative value.

you also have a typo in your code, the correct DataType is DateTime.

you can comparisons as you have for int, and doubles.

if(now < pastTime)
{

} 

CodePudding user response:

This will only open the specified form up to and including the 15th of the month:

if (DateTime.Now.Day <= 15)
{
    myForm.Show();
}

You can obviously change the operator and/or value to change the way it works. The important point is the Day property of the DateTime value representing the current date.

CodePudding user response:

The Error:

"; expected" error (in line 1 char 29).

You're missing a ending semi-colon, probably in a line above or below.

Tip: try to name variables well with meaning especially in anything-but-code (ABC) environments:

DateTime avoidReservedKeyWords = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 15);
  • Related