EmiliJ
asked on
Need help validating date YYYY-MM-DD
looking for a regex in .Net for validating a date parameter
with following attributes
Accepts dates with format yyyy-mm-dd
date should be between (1753-01-01 and 9999-12-31)(both inclusive)
Appreciate your help.
Thanks
with following attributes
Accepts dates with format yyyy-mm-dd
date should be between (1753-01-01 and 9999-12-31)(both inclusive)
Appreciate your help.
Thanks
What if you formatted your date using ToString as YYYYMMDD then check to see if its >= 17530101 and <= 99991231
date mask should be yyyyMMdd
if its a web application then i will recommend you to use a range validator
the sample is shown in the folowing EE link
https://www.experts-exchange.com/questions/22864495/Range-validator-problem-in-C-NET-to-check-date-range.html
the sample is shown in the folowing EE link
https://www.experts-exchange.com/questions/22864495/Range-validator-problem-in-C-NET-to-check-date-range.html
too much code :( ?
bool check(string s)
{
DateTime dt = DateTime.MinValue;
try
{
DateTime.ParseExact(s, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture);
//or try parse exact
}
catch (Exception ex)
{
return false;
}
if (dt < new DateTime(1753, 01, 01))
return false;
if (dt > new DateTime(9999, 12, 31))
return false;
return true;
}
an example
int iMyDate = Convert.ToInt32(dt.ToStrin g("yyyyMMd d"));
if (iMyDate >= 17530101 && iMyDate <= 99991231)
{
MessageBox.Show("Valid Date")
} else {
MessageBox.Show("Invalid") ;
}
int iMyDate = Convert.ToInt32(dt.ToStrin
if (iMyDate >= 17530101 && iMyDate <= 99991231)
{
MessageBox.Show("Valid Date")
} else {
MessageBox.Show("Invalid")
}
or that..
i know that is'n regex..
i know that is'n regex..
bool Check(string s)
{
DateTime dt = DateTime.MinValue;
bool result = DateTime.TryParseExact(s, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out dt);
if (!result || (dt < new DateTime(1753, 01, 01)) || (dt > new DateTime(9999, 12, 31)))
return false;
return true;
}
and final version..
good luck ;)
good luck ;)
/// <summary>
/// Check range and return result and dateTime.
/// </summary>
/// <param name="s">input string</param>
/// <param name="dateTime">DateTime.MinValue if not correct!</param>
/// <returns>true or false</returns>
bool Check(string s, out DateTime dateTime)
{
dateTime = DateTime.MinValue;
bool result = DateTime.TryParseExact(s, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out dateTime);
if (!result || (dateTime < new DateTime(1753, 01, 01)) || (dateTime > new DateTime(9999, 12, 31)))
return false;
return true;
}
ASKER
Thanks for all reply. actually I am developing a web service and using regex expressions for validating parameters. I wanted to be consistent and that the reason I wish to have regex to validate date ranges.
can you please try my suggestion for web using range validator sample using EE
ID:24029346
ID:24029346
You could use regex to perhaps check the format but your question of checking to see if its within a range, Im not sure if thats possible using regular expressions or is worth it. But you can use it to check for it to be in the format yyyyy-mm-dd
Im trying to get this expression to work but it seems to not recognise it as a valid sequence http://www.dotnetspider.com/forum/85373-regular-expression-for-date-format-yyyy-mm-dd.aspx
And in code I have to try it out is
Regex rex = new Regex("(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01] )");
if (rex.IsMatch("2001-11-11") .ToString( ))
{
MessageBox.show("So far date in valid format");
} else {
MessageBox.show("Date not in valid format");
}
But as I said, it is complaining about that expression in c#
Im trying to get this expression to work but it seems to not recognise it as a valid sequence http://www.dotnetspider.com/forum/85373-regular-expression-for-date-format-yyyy-mm-dd.aspx
And in code I have to try it out is
Regex rex = new Regex("(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01]
if (rex.IsMatch("2001-11-11")
{
MessageBox.show("So far date in valid format");
} else {
MessageBox.show("Date not in valid format");
}
But as I said, it is complaining about that expression in c#
I have the year portion of the regex validation functioning (I think). I got a bit lazy so I didn't do the date/day validation (but I'd imagine you know how to do that). If not, I can finish it for you. Anyway, you can remove the ?: if you don't care where any capture groups go.
(?:(?:1[7-9](?:(?:(?<=7)(?:[5-9][3-9]))|(?:(?<=[8-9])(?:\d\d))))|(?:[2-9]\d{3}))-\d{2}-\d{2}
-------------------------------
Test data (* represent pass):
1752-01-01
1822-01-01 *
1753-01-01 *
1794-01-01 *
8015-03-11 *
9999-12-31 *
10002-12-23
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.