Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
I want a validation for date in C# .net it cannot be future date and cannot be back dated more than one week. any one please help
Posted
Comments
deepankarbhatnagar 12-Jan-16 2:10am    
PLease explain more
Member 12078709 12-Jan-16 2:15am    
I am getting date from excel file i want to give validations to that field so i am giving validations in c# .net, here the date should not be greaterthan todays days date and also not be back dated more than one week(means if todays date is 12/01/2016 then user can give back date upto 05/01/2016)

You can write condition within your code to check the date


try this

C#
if (yourdate <=DateTime.Now && yourdate >=DateTime.Now.AddDays(-7))
            {
                
            }
 
Share this answer
 
v2
check DateTime.Today Property (System)[^] and DateTime.AddDays Method (Double) (System)[^]
You need to compare given date with DateTime.Today and Date in 7 days back. DateTime.AddDays method with minus value parameter will reduce the days from given date, so you can use that to find 7 days before date. other than that you need C# if conditions. Try yourself.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900