locked
How to convert string to datetime string str="14/09/2012 11:11 PM" RRS feed

  • Question

  • User1486092254 posted

    Hello guys,

    Can anyone help me to know to convert string to Datetime

            string str="14/09/2012 11:11 PM";

    I used parse excat

    like this:

                string format = "yyyy/dd/MM HH:mm tt";

                DateTime MyDateTime = DateTime.ParseExact(comp1, format, CultureInfo.InvariantCulture);

     but this not converting exactly format which I have mentioned. It is working for mm/dd/yyy

    so please help me soon anyone

    Monday, September 17, 2012 2:20 AM

Answers

  • User-1716253493 posted
    string str="14/09/2012 11:11 PM";            
    string format = "dd/MM/yyyy hh:mm tt";
    DateTime MyDateTime = DateTime.ParseExact(str, format, CultureInfo.InvariantCulture);
    


    the format depend your string value. If the str ="14-2012-09" you need format="dd-yyyy-MM"

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 17, 2012 2:30 AM

All replies

  • User-1716253493 posted
    string str="14/09/2012 11:11 PM";            
    string format = "dd/MM/yyyy hh:mm tt";
    DateTime MyDateTime = DateTime.ParseExact(str, format, CultureInfo.InvariantCulture);
    


    the format depend your string value. If the str ="14-2012-09" you need format="dd-yyyy-MM"

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, September 17, 2012 2:30 AM
  • User738333362 posted

    refer this..

    http://msdn.microsoft.com/en-us/library/system.datetime.aspx

    Use the DateTime Format to Get the Required Format date.

    Thanks.

    Monday, September 17, 2012 2:39 AM
  • User1410450975 posted
             string abc = "14/09/2012 11:11 PM";
                    abc = abc.Replace(" PM", "");
                    abc = abc.Replace("/", "-");
    
                    //DateTime dt = Convert.ToDateTime(abc);
                    //Console.WriteLine(dt.ToString("mm/dd/yy"));
                    DateTime MyDateTime;
                    MyDateTime = new DateTime();
                    MyDateTime = DateTime.ParseExact(abc, "dd-MM-yyyy HH:mm",  null);
                  Console.WriteLine(MyDateTime);

    Monday, September 17, 2012 2:44 AM
  • User1410450975 posted

    hey Oned_gk did you check your code if not then please check i think its show the message dateformat not valid

    Monday, September 17, 2012 2:47 AM
  • User-448512826 posted

    Hi,

    try...

    string strDate = txtDate.Text.Substring(6, 4) + "-" +
        txtDate.Text.Substring(3, 2) + "-" +
        txtDate.Text.Substring(0, 2);

    Monday, September 17, 2012 2:53 AM
  • User1486092254 posted

    Hi Sohail,

    you are right.

    it is showing the error message

    DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.

    Monday, September 17, 2012 2:54 AM
  • User1410450975 posted

    Please use my given code

    Monday, September 17, 2012 3:12 AM
  • User1486092254 posted

    I can't remove AM/PM Sohail.,

    AM/PM is must necessary. in my code i'm comparing two datetime man so..

    I want Am?pm also...

    my code output should be..

    * comparing two datetime

    * 1 is entered datetime, and one system datetime.

    * the entered datetime for textbox should not be early than the present datetime.

    Monday, September 17, 2012 3:22 AM
  • User-1716253493 posted

    Thank you Sohail,

    I have writed wrong format because i dont test it before.

    I just test it now and work after correct the format. Thanks.

    string str="14/09/2012 11:11 PM"; 
    string format = "dd/MM/yyyy hh:mm tt"; 
    DateTime MyDateTime = DateTime.ParseExact(str, format, CultureInfo.InvariantCulture);

    sandymj14 try my code now. I just change "AM" to "tt" for the format. It will work for you. Thanks

    Monday, September 17, 2012 3:34 AM
  • User1410450975 posted

    Then use 

    string abc = "14/09/2012 11:11 PM";
     DateTime MyDateTime = DateTime.ParseExact(abc, "dd/MM/yyyy hh:mm tt", null);
        Console.WriteLine(MyDateTime);



    Monday, September 17, 2012 3:44 AM
  • User1486092254 posted

    I did same code.. I post the same code when i', posting mu question. telling this code is not working for me..

    please check my question posted and give ur idea please

    Monday, September 17, 2012 4:55 AM
  • User1827126090 posted
    Try below code:
    // String to DateTime  String MyString;
    MyString = "1999-09-01 21:34 PM";
    DateTime MyDateTime;
    MyDateTime = new DateTime();
    MyDateTime = DateTime.ParseExact(MyString, "yyyy-MM-dd HH:mm tt", null);

    Monday, September 17, 2012 5:13 AM
  • User1410450975 posted

    Use my new code its working fine for me

    Monday, September 17, 2012 5:20 AM