none
DateTime.Parse

    Question

  • Hello everybody,
    Whenever i have to deal with datetime then before implementing my datetime.parse
    i always scratch my head becuase i think datetime.parse have to deal with system date format.
    Some times it goes fine in first attempt and some time it take time.

    So my question is this
    Is there any way to convert string into date irrespective of system date format to display as well as to save in DB ?
    Right now i wish to convert "16/04/07" to date time  
    As per my best knowledge format like "dd/MM/yy" is different thing it is like CSS to date but i wish to convert a string to datetime.

    first i try simple
    Code Block

    string testDate = "16/03/07";
    DateTime.Parse(testDate);  Error
    Convert.ToDateTime(testDate); Error

    int intYear = Convert.ToInt32(testDate(6, 2));
    int intMonth = Convert.ToInt32(testDate(3, 2));
    int intDay = Convert.ToInt32(testDate(0, 2));
    new DateTime(intYear, intMonth, intDay);    Error


    DateTime.Parse(testDate, DateTimeFormatInfo.InvariantInfo)   Error
    DateTime.Parse(testDate,"dd-MM-yy",null)   Error



    So how to deal with this.
    Which is the most suitable way to parse string to datetime irrespective of system date format?
    String may be like "16/03/07" or "03/16/07" or "16/03/2007" etc.
    also please tell me if red colored text/thoughts are wrong.
    Tuesday, December 04, 2007 8:49 AM

Answers

  • Well one problem is that the language can't tell what this date means by itself: 05/06/07

    it could be May 6th 2007, June 5th 2007, June 7th 2005, etc. ( those three are the most common formats of MM/dd/yy, dd/MM/yy and yy/MM/dd )
    So it will by default use the current culture setting to guess the format when using DateTime.Parse otherwise the programmer has to specify with DateTime.ParseExact and tell it explicitly what format the DateTime is expected to be in. You can override both with a culture info/datetime format info (DateTime.Parse docs).

    More info about parsing date times can be found here: Parsing Date and Time
    Tuesday, December 04, 2007 6:05 PM
  • Hi there,

     

    DateTime.Parse does not have to deal with the current System's date, it returns a DateTime object of the format you applied to it. So you have "16/03/07" which is a date that you want to convert to DateTime, you could use DateTime.Parse or DateTime.ParseExact.

     

    To save your DateTime after you convert it to DateTime, depending what DB you use, of course you can add that to the Database, every database library has DateTime support.

     

    Concerning DateTime.Parse, remember not cultures have the same Date Format, for instance... in NorthAmerica, we have "03/16/07" but in Europe they have "16/03/07", so we must apply a culture format so it will parse it automatically ... This is the correct way to do it. I am going to use the French Culture since they use that date formating.

     

    Code Block

    string testDate = "16/03/07";

    DateTime testDateTime = new DateTime();

    testDateTime = DateTime.Parse(testDate, new CultureInfo("fr-FR", true), DateTimeStyles.NoCurrentDateDefault);

    MessageBox.Show(testDateTime.ToLongDateString());

     

     

    You are correct about the date format you provided and one of the neat things about DateTime is that you can specify the format of the string you want to Parse Exactly! As the previous poster mentioned, you can use "DateTime.ParseExact".

     

    You just plug that in "the date format" that you believe is the exact format of the string you provided as the second parameter for ParseExact. And you will get the DateTime object that you need.

     

    Code Block

    string testDate = "16/03/07";

    DateTime testDateTime = new DateTime();

    testDateTime = DateTime.ParseExact(testDate, "dd/MM/yy", null);

    MessageBox.Show(testDateTime.ToLongDateString());

     

     

    That is it, now remember you cannot make a simple method guessing the dates for you... .NET DateTime uses the GregorianCalendar and DateTime can spot tthe difference between Year 08 and 2008 since if it contians 2 year digits it will use the current century if not, it uses the real year since GregorianCalendar. But the main difference is the formatting of Month and Day. If you put 02/01/2007 and 01/02/2007, that is a big difference but it means the same if you go from one country to another. You cannot do that in .NET unless you get the current culture of the Operating System and do it automatically.

     

    I hope that helped..

     

    Good Luck

    Tuesday, December 04, 2007 6:32 PM
  • Hello,

     

    If you wish to set a culture for the whole application, this is how you will do it...

     

    Code Block

    // Set the global culture for the application to be France

    Application.CurrentCulture = new CultureInfo("fr-FR", true);

     

    // Current Date we wish to convert

    string testDate = "16/03/07";

     

    // Parsing the date normally using Parse with no parameter

    DateTime testDateTime = DateTime.Parse(testDate);

     

    // Lets prove it

    MessageBox.Show(testDateTime.ToLongDateString());

     

     

    Remember, setting the application as a different culture will make the application act as if it was that culture .. So number format will change, dateformat will change, everything that culture seems normal to you but not to them will change. For instance instead of saying "Friday" it will say "Vendredi". Because that is what Culture does. I beleive it works per thread.

     

    Now, if you wish to leave the functionality as your culture but only dateformat changed, you can pass in the culture parameter instead to the Parse method, that will allow you to use your culture instead with a custom date culture.

     

    Note you can alter your own culture's datetime differently by adding a default datetime in Culture Info

     

    I hope that helped ,

     

    Good Luck

    Wednesday, December 05, 2007 4:35 PM

All replies

  • please take a look at DateTime.ParseExact method.
    Tuesday, December 04, 2007 4:27 PM
  • Well one problem is that the language can't tell what this date means by itself: 05/06/07

    it could be May 6th 2007, June 5th 2007, June 7th 2005, etc. ( those three are the most common formats of MM/dd/yy, dd/MM/yy and yy/MM/dd )
    So it will by default use the current culture setting to guess the format when using DateTime.Parse otherwise the programmer has to specify with DateTime.ParseExact and tell it explicitly what format the DateTime is expected to be in. You can override both with a culture info/datetime format info (DateTime.Parse docs).

    More info about parsing date times can be found here: Parsing Date and Time
    Tuesday, December 04, 2007 6:05 PM
  • Hi there,

     

    DateTime.Parse does not have to deal with the current System's date, it returns a DateTime object of the format you applied to it. So you have "16/03/07" which is a date that you want to convert to DateTime, you could use DateTime.Parse or DateTime.ParseExact.

     

    To save your DateTime after you convert it to DateTime, depending what DB you use, of course you can add that to the Database, every database library has DateTime support.

     

    Concerning DateTime.Parse, remember not cultures have the same Date Format, for instance... in NorthAmerica, we have "03/16/07" but in Europe they have "16/03/07", so we must apply a culture format so it will parse it automatically ... This is the correct way to do it. I am going to use the French Culture since they use that date formating.

     

    Code Block

    string testDate = "16/03/07";

    DateTime testDateTime = new DateTime();

    testDateTime = DateTime.Parse(testDate, new CultureInfo("fr-FR", true), DateTimeStyles.NoCurrentDateDefault);

    MessageBox.Show(testDateTime.ToLongDateString());

     

     

    You are correct about the date format you provided and one of the neat things about DateTime is that you can specify the format of the string you want to Parse Exactly! As the previous poster mentioned, you can use "DateTime.ParseExact".

     

    You just plug that in "the date format" that you believe is the exact format of the string you provided as the second parameter for ParseExact. And you will get the DateTime object that you need.

     

    Code Block

    string testDate = "16/03/07";

    DateTime testDateTime = new DateTime();

    testDateTime = DateTime.ParseExact(testDate, "dd/MM/yy", null);

    MessageBox.Show(testDateTime.ToLongDateString());

     

     

    That is it, now remember you cannot make a simple method guessing the dates for you... .NET DateTime uses the GregorianCalendar and DateTime can spot tthe difference between Year 08 and 2008 since if it contians 2 year digits it will use the current century if not, it uses the real year since GregorianCalendar. But the main difference is the formatting of Month and Day. If you put 02/01/2007 and 01/02/2007, that is a big difference but it means the same if you go from one country to another. You cannot do that in .NET unless you get the current culture of the Operating System and do it automatically.

     

    I hope that helped..

     

    Good Luck

    Tuesday, December 04, 2007 6:32 PM
  • Hello everybody,
    Very intresting thoughts

    Thanks for your advice
    But again few questions comes into my mind
    Mr m0 mention about cultureinfo and 01/02/07(may be mm/dd/yy oe dd/mm/yy)
    Now if i hard code it any culture then how it will behave from culture to culture?
    I guess date format error will come.
    (because recetly i faced similar problem)
    I develop my APP in india, client is of UK, So i set culture info as per client location
    to meet clients requirments.
    But this application will definatly not work fine in some other culture

    Is there any way to define culture info dynamically?
    Is there any example like i mention in my first post as
    intYear = ConvertTo.Datetime(strTest.Substring(6,2)
    ..................
    .................
    So that it automatically handle dates irrespected of location/Culture.
    Wednesday, December 05, 2007 7:38 AM
  • first u put the month after that date and year like string testDate = "03/16/2007";

    check this out
    Wednesday, December 05, 2007 7:59 AM
  • Well there is a concept of UTC where you use a Universal Time specific format and only adjust when you display the date to the user. This has the advantage that the time isn't in a different format for different regions of the would.

    As for something that handles dates of different format, as was stated before, the amount of information and intuition used to determine what format the date is in makes it impractical to develop code to perform the parsing. Either a lack of information or an ambiguous date would occur. If you could store the culture information with the date-time string, then you could write a parse method that will be able to parse any of the date-time formats.
    Wednesday, December 05, 2007 4:16 PM
  • Hello,

     

    If you wish to set a culture for the whole application, this is how you will do it...

     

    Code Block

    // Set the global culture for the application to be France

    Application.CurrentCulture = new CultureInfo("fr-FR", true);

     

    // Current Date we wish to convert

    string testDate = "16/03/07";

     

    // Parsing the date normally using Parse with no parameter

    DateTime testDateTime = DateTime.Parse(testDate);

     

    // Lets prove it

    MessageBox.Show(testDateTime.ToLongDateString());

     

     

    Remember, setting the application as a different culture will make the application act as if it was that culture .. So number format will change, dateformat will change, everything that culture seems normal to you but not to them will change. For instance instead of saying "Friday" it will say "Vendredi". Because that is what Culture does. I beleive it works per thread.

     

    Now, if you wish to leave the functionality as your culture but only dateformat changed, you can pass in the culture parameter instead to the Parse method, that will allow you to use your culture instead with a custom date culture.

     

    Note you can alter your own culture's datetime differently by adding a default datetime in Culture Info

     

    I hope that helped ,

     

    Good Luck

    Wednesday, December 05, 2007 4:35 PM