Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all

string item1 = "06/14/2012";(MM/dd/yyyy)formate

//this is validate the year using Regex
 if (Regex.IsMatch(date, @".*/\s*2012", RegexOptions.CultureInvariant))
{
//adding year
}
//if i want to validate month how can i do.?
//in item1 is datetime.
//if i validate item1 how can i know this is particular month
//?


in item1 is datetime.
if i validate item1 how can i know this is particular month?


here i want to validate only months using Regex.


how can i do this functionality.

let me know......
Posted
Updated 14-Jun-12 1:51am
v2

Why not use the DateTime.TryParseExact[^] method to validate the date.

C#
string date = "06/14/2012"; //(MM/dd/yyyy)format
            DateTime dateResult;

            if (DateTime.TryParseExact(date, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateResult))
            {
                Console.WriteLine("This is a valid date.");
            }
 
Share this answer
 
v3
Comments
U@007 14-Jun-12 7:52am    
hi Manas bhardwaj

in item1 is datetime.
if i validate item1 how can i know this is particular month?
AmitGajjar 14-Jun-12 7:54am    
using dateResult.Month
U@007 14-Jun-12 8:39am    
Thank U bhardwaj
5+.
VJ Reddy 14-Jun-12 8:49am    
Good answer. 5!
Manas Bhardwaj 14-Jun-12 9:36am    
thx!
I wouldn't use a regex at all - it is a whole lot easier to validate the lot with a DateTime.tryParseExact:
C#
string s = "06/14/2012";
DateTime dt;
if (DateTime.TryParseExact(s, "MM/dd/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
    {
    }

Anything you don't specify in the format string will be defaulted.
 
Share this answer
 
Comments
U@007 14-Jun-12 7:53am    
hi OriginalGriff

in item1 is datetime.
if i validate item1 how can i know this is particular month?
OriginalGriff 14-Jun-12 8:07am    
item1 is a string containing a string representation of a date - it is not a DateTime which is a specific .NET classs to handle dates. If you parse it to a DateTime, then you can get all of the information you want via the DateTime properties. These include Month, Year, Day and also allow you to compare two dates directly, or subtract one from the other to get the TimeSpan interval.
It is well worth converting to a DateTime as early as possible, and converting back to a string as late as possible - doing comparisons and math on string based dates is a pain in the arse! :laugh:
U@007 14-Jun-12 8:40am    
yah it's obviously right mr originalgriff
Thank U my side
5+.
OriginalGriff 14-Jun-12 9:29am    
You're welcome!
Manas Bhardwaj 14-Jun-12 7:54am    
ditto +5!
You can use this regex for validating month: "^(0?[1-9]|1[012])$"
 
Share this answer
 
Comments
U@007 14-Jun-12 7:50am    
hi vani Kulkarni

in item1 is datetime.
if i validate item1 how can i know this is particular month?
Hi,

Best way of validating date is to use Parse, TryParse or TryParseExact instead validating through string.

you can use something like,
C#
string date = "12/12/2012";
DateTime result = default(DateTime);
DateTime.TryParse(date, out result);


after getting datetime in result , you can fetch anything from parsed date.

Thanks
-Amit
 
Share this answer
 
v2

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