locked
Datetime Convertion RRS feed

  • Question

  • hi,

    i have some plain text that i want to convert to datetime by c#.

    For example, i have 0105100415 (01->day, 05->month, 10->year, 04->hour, 15->minute). I want to convert this plain text to datetime type. Is there any general way that i can do this convertion like Convert.ToDateTime(strPlain,"ddmmyyHHMM") ??

    I need a general format because i have other plain texts too like 10-02-27,18:25 (10->year, 02->month, 27->day .......).

     

    Can you help me with this?

     

    Thanks.

    Thursday, May 13, 2010 5:59 AM

Answers

  • You can use ParseExact to convert any string to a date, if you know its format.
    DateTime d1 = DateTime.ParseExact("0105100415", "ddMMyyhhmm", CultureInfo.CurrentCulture);
    DateTime d2 = DateTime.ParseExact("10-02-27,18:25", "yy-MM-dd,HH:mm", CultureInfo.CurrentCulture);
    • Marked as answer by zaferaydn Sunday, May 16, 2010 6:26 AM
    Saturday, May 15, 2010 8:47 PM

All replies

  • hi. zaferaydn

    i think there is no method which is able to convert this type of string in datetime..

    but you can convert it with some tweaks like-

                String dd = "0105100415";
                char[] pp = dd.ToCharArray(0,dd.Length);
                int day = Convert.ToInt16(pp[0].ToString() + pp[1].ToString());
                int month = Convert.ToInt16(pp[2].ToString() + pp[3].ToString());
                int year = Convert.ToInt16(pp[4].ToString() + pp[5].ToString());
                int hour = Convert.ToInt16(pp[6].ToString() + pp[7].ToString());
                int min = Convert.ToInt16(pp[8].ToString() + pp[9].ToString());
                DateTime newdate = new DateTime(year, month, day, hour, min, 0);
                newdate = Convert.ToDateTime(newdate.ToString("dd/mm/yy hh:mm"));

     

    regards

    jayant

     

     

    Thursday, May 13, 2010 7:45 AM
  • You can convert string to DateTime object using TryParse or Parse method in DateTime class

     ( for reference http://msdn.microsoft.com/en-us/library/ch92fbc1.aspx )

    see this example

    string[] dateStrings = {"05/01/2009 14:57:32.8", "2009-05-01 14:57:32.8", 
                "2009-05-01T14:57:32.8375298-04:00", 
                "5/01/2008 14:57:32.80 -07:00", 
                "1 May 2008 2:57:32.8 PM", "16-05-2009 1:00:32 PM", 
                "Fri, 15 May 2009 20:10:57 GMT" };
    DateTime dateValue;
    
    Console.WriteLine("Attempting to parse strings using {0} culture.", 
             CultureInfo.CurrentCulture.Name);
    foreach (string dateString in dateStrings)
    {
      if (DateTime.TryParse(dateString, out dateValue)) 
       Console.WriteLine(" Converted '{0}' to {1} ({2}).", dateString, 
                dateValue, dateValue.Kind);
      else
       Console.WriteLine(" Unable to parse '{0}'.", dateString);
    }
    // The example displays the following output:
    //  Attempting to parse strings using en-US culture.
    //    Converted '05/01/2009 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
    //    Converted '2009-05-01 14:57:32.8' to 5/1/2009 2:57:32 PM (Unspecified).
    //    Converted '2009-05-01T14:57:32.8375298-04:00' to 5/1/2009 11:57:32 AM (Local).
    //    Converted '5/01/2008 14:57:32.80 -07:00' to 5/1/2008 2:57:32 PM (Local).
    //    Converted '1 May 2008 2:57:32.8 PM' to 5/1/2008 2:57:32 PM (Unspecified).
    //    Unable to parse '16-05-2009 1:00:32 PM'.
    //    Converted 'Fri, 15 May 2009 20:10:57 GMT' to 5/15/2009 1:10:57 PM (Local).
    
    

    Thanks Mike -------- Mark best answers as Answer ----------
    • Proposed as answer by insigniya Thursday, May 13, 2010 9:31 AM
    Thursday, May 13, 2010 8:42 AM
  • You can use ParseExact to convert any string to a date, if you know its format.
    DateTime d1 = DateTime.ParseExact("0105100415", "ddMMyyhhmm", CultureInfo.CurrentCulture);
    DateTime d2 = DateTime.ParseExact("10-02-27,18:25", "yy-MM-dd,HH:mm", CultureInfo.CurrentCulture);
    • Marked as answer by zaferaydn Sunday, May 16, 2010 6:26 AM
    Saturday, May 15, 2010 8:47 PM