locked
String was not recognizes as a valid datetime RRS feed

  • Question

  • As the title says.. it doesnt recognize the following string as a datetime.

    "31/01/2012 10:00 AM"

    I would like to convert this string to a datetime with the following format. yyyy/MM/dd hh:mm:ss

    Any ideas?

     

    Greetings Spacelama

     

    Monday, January 30, 2012 11:14 AM

Answers

  • Hi,

    Try this:

    string

     

    strDate = "31-01-2012 10:00:00 AM"

    ;

     

    var dtfi = new DateTimeFormatInfo { ShortDatePattern = "dd-MM-yyyy HH:mm:ss", DateSeparator = "-"

    };

     

    DateTime objDate = Convert.ToDateTime(strDate, dtfi);

    After doing this you can display it in any format like this:

    string

     

    str = objDate.ToString("yyyy/MM/dd HH:mm:ss"

    );

     

     

     

      

    Regards

    Mahesh

     

      

       
    • Proposed as answer by MaheshKS Monday, January 30, 2012 12:55 PM
    • Marked as answer by Leo Liu - MSFT Monday, February 6, 2012 3:46 AM
    Monday, January 30, 2012 12:36 PM
  • Hi Sapce Lama..

    try this

      String dateString = "31/01/2012 10:00 AM";
                string[] str = dateString.Split(new Char[] {' '});
                string[] strDate = str[0].Split(new char[] { '/' });
                string strResultDate = strDate[2] + '/' + strDate[1] + "/" +  strDate[0] + ' ';
                strResultDate = strResultDate + str[1] + ' ' + str[2];
                MessageBox.Show(strResultDate);
    

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"
    Monday, January 30, 2012 12:54 PM
  • Hi sapcelama,

                 Try the following thread link about datetime conversion.

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ef057805-c535-4d14-877a-41a268ccec7d


    If a post answers your question, please click Mark As Answer on that post and Mark as Helpful. Happy Coding...
    Monday, January 30, 2012 1:03 PM
  • You can check following code

     // Your given format
                string strDate = "31/01/2012 10:00 AM";
                // need to add using System.Globalization; namespace
                DateTime dt = DateTime.ParseExact(strDate, "dd/MM/yyyy hh:mm tt", CultureInfo.InvariantCulture);
                // Your desired format
                MessageBox.Show(dt.ToString("yyyy/MM/dd hh:mm:ss"));
    

    Hope it will help you


    Hasibul Haque,MCC,MCPD hasibulhaque.com
    • Proposed as answer by JohnGrove Monday, January 30, 2012 7:40 PM
    • Marked as answer by Leo Liu - MSFT Monday, February 6, 2012 3:47 AM
    Monday, January 30, 2012 1:20 PM

All replies

  • I recommend creating a Datetime object (use your values 31/03/2012 to intialize it in code, can be done dynamically as well) and apply formatting to the datetimeObject.ToString() , the Datatime format specifier is available here 

    http://www.csharp-examples.net/string-format-datetime/

    Monday, January 30, 2012 11:41 AM
  • Well this tutorial says how to convert DateTime to a string, but i need to convert a string to a DateTime.

    Convert.ToDateTime(string);

    doesnt work, because it is not recognized...

    Monday, January 30, 2012 11:59 AM
  • Hi,

    Try this:

    string

     

    strDate = "31-01-2012 10:00:00 AM"

    ;

     

    var dtfi = new DateTimeFormatInfo { ShortDatePattern = "dd-MM-yyyy HH:mm:ss", DateSeparator = "-"

    };

     

    DateTime objDate = Convert.ToDateTime(strDate, dtfi);

    After doing this you can display it in any format like this:

    string

     

    str = objDate.ToString("yyyy/MM/dd HH:mm:ss"

    );

     

     

     

      

    Regards

    Mahesh

     

      

       
    • Proposed as answer by MaheshKS Monday, January 30, 2012 12:55 PM
    • Marked as answer by Leo Liu - MSFT Monday, February 6, 2012 3:46 AM
    Monday, January 30, 2012 12:36 PM
  • Hi Sapce Lama..

    try this

      String dateString = "31/01/2012 10:00 AM";
                string[] str = dateString.Split(new Char[] {' '});
                string[] strDate = str[0].Split(new char[] { '/' });
                string strResultDate = strDate[2] + '/' + strDate[1] + "/" +  strDate[0] + ' ';
                strResultDate = strResultDate + str[1] + ' ' + str[2];
                MessageBox.Show(strResultDate);
    

     


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful"
    Monday, January 30, 2012 12:54 PM
  • Hi sapcelama,

                 Try the following thread link about datetime conversion.

    http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/ef057805-c535-4d14-877a-41a268ccec7d


    If a post answers your question, please click Mark As Answer on that post and Mark as Helpful. Happy Coding...
    Monday, January 30, 2012 1:03 PM
  • You can check following code

     // Your given format
                string strDate = "31/01/2012 10:00 AM";
                // need to add using System.Globalization; namespace
                DateTime dt = DateTime.ParseExact(strDate, "dd/MM/yyyy hh:mm tt", CultureInfo.InvariantCulture);
                // Your desired format
                MessageBox.Show(dt.ToString("yyyy/MM/dd hh:mm:ss"));
    

    Hope it will help you


    Hasibul Haque,MCC,MCPD hasibulhaque.com
    • Proposed as answer by JohnGrove Monday, January 30, 2012 7:40 PM
    • Marked as answer by Leo Liu - MSFT Monday, February 6, 2012 3:47 AM
    Monday, January 30, 2012 1:20 PM
  • Thanks guys,

    Is it possible to send a DateTime with the following format: yyyy/MM/dd hh:mm:ss ? I get this format into a string, but it seems that DateTime always be in this format: MM/dd/yyyy hh:mm:ss ?

     

    Greetings SpaceLama

    Monday, January 30, 2012 5:19 PM
  • Actually where you want to send? DB?

    Your Date time formate depends on your pcs culture nature.

    You can change this format by changing your date time format from your System(Regional Settings). Or changing your current culture nature.


    Hasibul Haque,MCC,MCPD hasibulhaque.com
    Monday, January 30, 2012 5:42 PM