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#.
Asked
Active
Viewed 2,835 times
-1
-
3What does "exactly a month" mean to you? – Kenneth K. Nov 18 '15 at 16:47
-
1Have you read the `DateTime` documentation? – xxbbcc Nov 18 '15 at 16:48
-
a month, like if today is 18, I´d like the start date to be at least a month before today, 18 october for example. – L. de la Cruz Nov 18 '15 at 16:49
-
Where can i find the documentation. – L. de la Cruz Nov 18 '15 at 16:50
-
Look at the answer from Jon Skeet here: http://stackoverflow.com/questions/1525990/calculating-the-difference-in-months-between-two-dates – Fruchtzwerg Nov 18 '15 at 16:50
-
https://msdn.microsoft.com/en-us/library/system.datetime%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 (DateTime on MSDN) Documentation on DateTime – Whiplash450 Nov 18 '15 at 16:54
2 Answers
0
Do you mean like this date == DateTime.Today.AddMonths(-1);
?

coding lion
- 51
- 3
-
1Just 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