-1

I'm making a program for project control, and I´d like it to validate the start date using the condition that it has to be atleast a month before today. I pretty much have no idea what im doing when it comes to datetime so any help would be appreciated. And before you ask, no it´s not homework, I'm teaching myself c#.

L. de la Cruz
  • 35
  • 1
  • 6

2 Answers2

0

Do you mean like this date == DateTime.Today.AddMonths(-1);?

  • 1
    Just to clarify, this will only match if `date` is also truncated to the `.Today` part, if it includes a legitimate time part, this check will fail. – Ron Beyer Nov 18 '15 at 16:50
  • @RonBeyer "Truncate" is misleading. You can't truncate time in .NET. You can only set it to a specific time--midnight in this case. (Nit-picky, I know.) – Kenneth K. Nov 18 '15 at 17:06
0
DateTime today = DateTime.Now.Date
DateTime oneMonthAgo = today.AddMonth(-1);
bool isOk = startDate <= oneMonthAgo; // or < ? define exact condition for your requirement

DateTime class has plenty of useful method to deal with Date in many way: use them!

Gian Paolo
  • 4,161
  • 4
  • 16
  • 34