views:

397

answers:

7

I have a situation in which I need to convert a text data into date.

I have used the following code to do so!


        string s = textBox1.Text;
        if (String.IsNullOrEmpty(s))
        {
            textBox2.Text = "Please enter any date.";

        }
        else
        {
            DateTime dt = DateTime.Parse(s);
            string day = dt.Day.ToString();
            string month = dt.Month.ToString();
            string year = dt.Year.ToString();
            s = day + "/" + month + "/" + year;
            textBox2.Text = s;
        }


This will change only the following formats of data.


10/10/09 or 10/10/2009----converted date------10/10/2009

12/13/2009 or 12/13/9----converted date-------12/13/2009

10/16----converted date-----------------------16/10/2009

2009/12/10----converted date------------------10/12/2009


The following formats are not getting changed to dd/mm/yyyy


16/10-dd/mm

060209 or 06022009 ddmmyyyy

13122009 mmddyyyy

20091213 yyyymmdd

20091312 yyyyddmm

20.07.2009/20.07.2009/20-07-2009/20-07-09


Can anyone help me out with this. I am very new to c#

+1  A: 

Parsing dates is a bit more complicated than that, I'm afraid. It will depend on you culture/country settings. Look at the overloads for DateTime.Parse...

Also note that when you output your date, you could/should also use String.Format, like so:

String.Format("{0:dd/MM/yyyy}", dt)
Benjol
A: 

Firstly, it would probably be more elegant if you were to use the DateTime.ToString method with a format string specifying the form that you want the date string to appear in (information on this can be found here).

As for why the other formats that you specified are not being parsed into a DateTime object correctly, you probably need to use the DateTime.Parse(string, IFormatProvider) variant and define your own IFormatProvider for this. You might find this MSDN article might be useful for this.

jpoh
A: 

You will have to write your own parsing for all non standard formats. Also formats like "yyyymmdd" and "yyyyddmm" will be problematic, since you will need user to specify what's the input format or you will just convert in wrong in some cases.

For the rest take a look at DateTime.Parse (Also mentioned by Benjol) it should solve your problem for common formats used throughout the world.

Rekreativc
A: 

You can use DateTime.ParseExact

For example:

DateTime.ParseExact("20090823", "yyyyMMdd", null);    
DateTime.ParseExact("16/10", "dd/MM", null);

Edit: Use DateTime.TryParse (or DateTime.TryParseExact) to validate the date entered.

DateTime date;
string error = "";
if (!DateTime.TryParse("12978434", out date))
{
    error = "Invalid date";
}
Castrohenge
This doesn't work. I think you have to specify an appropriate IFormatProvider or ICultureInfo.
jpoh
I had this working with null on my machine. Think I'll do some more investigation on this one.
Castrohenge
A: 

DateTime.Parse cannot possibly parse all the combinations that you specify. I would suggest you first try DateTime.TryParse to see if the input date is parse-able. If that returns false, then use ParseExact (or better yet, TryParseExact) to check against each expected date format.

Unfortunately I don't think there's a one-size-fits-all solution to this problem.

Amit G
A: 

Is it possible for you to use a 'DatePicker' / Calendar control instead of a TextBox?

eg AJAX Toolkit Calendar control

This would remove the need for you to have to handle every possible date format a user could potentially specify. It also provides a nicer user experience IMHO.

Update after OP comment:

As other posters have suggested, I would use DateTime.TryParse as a first pass, to handle date strings in any of the supported standard formats. Then you would need to fallback on DateTime.ParseExact for each supported non-standard format. Finally I guess you need to flag an error for any non-date strings or unspported date formats.

Amal Sirisena
I agree, But can I want this code as a part of validation to pick text data from text file and check.
Srikanth V M
Ok I see, I thought I would check to see if you could get away with an easier approach :-)
Amal Sirisena
A: 
Srikanth V M