I need to perform some date operations in ASP.net using C#.
-
The date i would enter should be of format ‘Jul-05’ (mmm-yy Format and type-string)… how can i check with this???? Or how can i validate this with whatever user is entering as a string???
-
After validating that, i need to compare tht with a value in Database(say a column name buy_period which has a value (say) 04/31/2007).
How can i write a Query for comparing both?? (as both dates would be of different formats)
Can u pls help me in this ???
Use the
TryParseExactmethod to validate the string and parse it to aDateTimevalue:The
DateTimevalue will use the first day of month and the time 0:00 to fill up a complete value, so a string like'jul-05'will be parsed into a completeDateTimevalue like2005-07-01 00:00:00.0000, so it will be the starting point of that month.To compare this to a date in the database you also need the starting point of the next month, which you get with:
Now you can just compare a date to the starting and ending point of the month in this manner: