0

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
  • 1
    Is it a string field? In that case you might need to extract the date information to use for DateTime.parse method
    – Raul
    Commented Nov 8, 2016 at 7:54

2 Answers 2

3

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);
System.debug(result);

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.

0
string s = '2016-11-08T13:04:16.5260175Z';
s = s.substring(0,s.indexOf('.'));
s= s.replace('T',' ');
datetime d= datetime.valueof(s);
3
  • 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
    Commented Nov 8, 2016 at 9:38
  • edited the code.
    – umithuckan
    Commented Nov 8, 2016 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
    Commented Nov 8, 2016 at 10:09

You must log in to answer this question.

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