Please help. I want to assign this datetime which is string format to datetime data type. 6/2/2022 12:00:00 AM

I tried Datetime dt = DateTime.parse('10/14/2011 11:46:00 AM'); but it is giving error System.TypeException: Invalid date/time: 10/14/2011 11:46:00 AM. Please help

Expected Output in DateTime variable: 10/14/2011 11:46 AM

2 Answers 2


The first thing that I noticed is a missing comma after the date value, so the string date value should be '10/14/2011, 11:46:00 AM'

You might also need to enable the ICU locale formats, please visit below link for more information


  • Thanks for the response. I am abble to parse Datetime dt = DateTime.parse('10/14/2011 11:46 AM'); but I need 10/14/2011 11:46:00 AM this date not 10/14/2011 11:46 AM. For this what shoild I do?
    – Rene
    Commented Jun 10, 2022 at 11:22
  • Do you need the date part only without time or date and time both? I couldn't understand the ask now. Commented Jun 10, 2022 at 11:29
  • I need all of them. It is just that it is in string format but I need to assign it to DateTime variable and when I am assigning it , it is throwing exception. I need 10/14/2011 11:46 AM as an output when it is stored in DateTime variable.
    – Rene
    Commented Jun 10, 2022 at 11:43
  • And one last query, you need it with seconds since I am able to parse the input without seconds. 10/14/2011 11:46:00 AM, if I remove the 00 from seconds then it is getting parsed. If without seconds is ok use the regex that removes the seconds from the string which can then be parsed. Commented Jun 10, 2022 at 11:51

Ideally, you'll want to use Datetime.valueOf(stringDate) or Datetime.valueOfGmt(stringDate) where stringDate is of the format yyyy-MM-dd HH:mm:ss

If you do not have control over the creation of the text string that you are converting, then we'll need to convert it to yyyy-MM-dd HH:mm:ss Is your incoming string always fixed length (having two digits for each of the parts (MM/dd/yyyy HH:mm:ss AP)? If so, then it would be rather straightforward to slice the string into parts and then reconstruct into the new format. The one caveat being that you would also need to look at the AM/PM and add 12 to the hours if it is PM. If the incoming string is not guaranteed to have double-digits, then it can still be done, just need to use split to break the string into parts.

String strDT = '10/14/2011 11:46:12 PM';
String[] arrDT = strDT.split('[/ :]');
Integer year = Integer.valueOf(arrDt[2]);
Integer month = Integer.valueOf(arrDt[0]);
Integer day = Integer.valueOf(arrDt[1]);
Integer hour = Integer.valueOf(arrDt[3]) + ( arrDt[6] == 'PM' ? 12 : 0 );
Integer minute = Integer.valueOf(arrDt[4]);
Integer second = Integer.valueOf(arrDt[5]);
DateTime dt = DateTime.newInstance(year, month, day, hour, minute, second);

EDIT: Another option would be to strip the seconds from the original string, parse it, then add the seconds value to the DateTime variable.

  • Hi Paul, Thanks for the answer but I am not sure how it can be done. Could you please let me know about the slice method?
    – Rene
    Commented Jun 13, 2022 at 5:38
  • @Rene - I updated my answer with working code that breaks down the text string and converts to DateTime.
    – Paul N
    Commented Jun 13, 2022 at 17:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .