I'm getting Invalid date/time: 2018-09-18T16:57:37Z in the developer console. please help me with this

DateTime dt = Datetime.now().addMinutes(-10);     
String formatedDt = dt.formatGMT('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
DateTime myDateTime = DateTime.valueOf(formatedDt);
 system.debug('date  *****'+myDateTime); 
  • Please edit your post to describe clearly which line is throwing the error. Sep 18, 2018 at 22:24

1 Answer 1


dt.formatGMT('yyyy-MM-dd\'T\'HH:mm:ss\'Z\''); formats the date in a way that is incompatible with the DateTime.valueOf() method.

DateTime.valueOf() expects the string to be formatted as yyyy-MM-dd HH:mm:ss and in the local timezone. Since your string isn't formatted that way, you're getting an error when that line runs.

To get the correct format you should update this line

String formatedDt = dt.formatGMT('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');

to this:

String formatedDt = dt.formatGMT('yyyy-MM-dd HH:mm:ss');

  • Thanks @john towers than how I will get this format yyyy-MM-ddTHH:mm:ssZ from yyyy-MM-dd HH:mm:ss
    – Sriram
    Sep 18, 2018 at 22:43
  • @Sairam I've updated the answer with what should be changed to produce the correct format. Sep 18, 2018 at 22:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.