I need to convert "2016-11-08T13:04:16.5260175Z" this one into DateTime format like "11/8/2016 1:04 PM". Please help me.

  • 1
    Is it a string field? In that case you might need to extract the date information to use for DateTime.parse method – Raul Nov 8 '16 at 7:54

I think that the most reliable method is to use the JSON.deserialize method. This accounts for time zones etc and is invoked like this:

DateTime result = (DateTime)JSON.deserialize('"' + '2016-11-08T13:04:16.5260175Z' + '"', DateTime.class);

Note that I've put an extra set of double quotes around the string you want to parse. JSON is going to expect those, and you'll get odd results without them.

string s = '2016-11-08T13:04:16.5260175Z';
s = s.substring(0,s.indexOf('.'));
s= s.replace('T',' ');
datetime d= datetime.valueof(s);
  • You may want to explain what it is you're actually doing here. This wouldn't even compile because you're declaring two strings, s, with the same name. – Dan Jones Nov 8 '16 at 9:38
  • edited the code. – umithuckan Nov 8 '16 at 10:01
  • 3
    Try explaining the code too? What does s.substring do for someone who's never used it before? What does s.replace do? What are we doing when setting Datetime d? – Dan Jones Nov 8 '16 at 10:09

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.