Click here to Skip to main content
15,904,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i'm Working in Windows Application. I'm Developing Simple Notification System based c#.net with datetime concept. this project working in my system. i need validate datetime as dd/MM/yyyy while validate in client system error occur as string not recognized as a valid datetime. becoz client system have system formatted : dd MMMM yyyy

let me know how to accept all kind of datetime format in my project..

Used Code

C#
public bool DateValidation(string stringDateValue)
        {
            bool A = false;
            try
            {
                CultureInfo CultureInfoDateCulture = new CultureInfo("fr-FR");
                DateTime d = DateTime.ParseExact(stringDateValue, "dd/MM/yyyy", CultureInfoDateCulture);
                A = true;
            }
            catch
            {
               
                        A = false;
            }
        

            return A;
        }
Posted

Don't assume any format: just use the current format and that should be what the user entered (since that's what he likes to use):
C#
public bool DateValidation(string stringDateValue)
    {
    DateTime d;
    return DateTime.TryParse(stringDateValue, out d);
    }
Or better still, use a DateTimePicker and you don't need to validate it as a valid DateTime as the user can't enter a "bad" one.
 
Share this answer
 
Comments
Brinda Arumugam 26-Jul-14 2:50am    
thanks for reply ...
but i want to validate date in datagridview cell.so we should tell to client whats the format or any other solution for datagridview datetime cell ?
OriginalGriff 26-Jul-14 2:57am    
Depends on *where* the data came from: the source should be a DateTime value (i.e. the DataTable or whatever you used as the source should have a DateTime column) in which case it is already valid, and just needs presenting in the current users preferred format - which happens automatically.

The problem only occurs when the data source is not a DateTime: if for example a database has date information stored as a string. In that case, you are in real trouble: you don;t know what format the data was entered in when it was stored so you can't validate it as a valid date now.

For example, if the string is "31/2/16" is that 16th Feb 2031, or 16th Feb 1931, or 31st Feb 2016? Only the last is invalid as a DateTime!

So, where does the data come from?
Brinda Arumugam 26-Jul-14 3:55am    
Date come from datagridview cell

this s my code
<pre lang="c#">
if (e.ColumnIndex == 1)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == DBNull.Value || dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.Equals(""))
{ }
else
{
String cleanedString = System.Text.RegularExpressions.Regex.Replace(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString(), @"\s+", " ");
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = cleanedString.Trim();

if (T.DateValidation(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString()) == false)
{
MessageBox.Show("Invalid Date Format!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = "";
}
}
}
</pre>

When i get date from datagridview cell that time only this error occur...
OriginalGriff 26-Jul-14 4:13am    
No, I meant where did the datagridview cell get the data from in the first place - it's probably too late buy the time it's in the DGV!
C#
DateTime d = DateTime.ParseExact(stringDateValue,  new string[] { "dd/MM/yyyy", "MM/dd/yyyy", "dd MMMM yyyy" }, CultureInfoDateCulture, DateTimeStyles.None);
 
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