2

DateTime.parse() won't take seconds or offsets, at least not in the US. The format from the documention in DateTime.methods() is the only one I can find that works:

Datetime dt = DateTime.parse('10/14/2011 11:46 AM');

I haven't found documentation stating this, and in some places I've gotten error messages suggesting that I need to use "mm/dd/YYYY HH:mm:ss:nnnZ+HH.mm", which I have tried extensively without success. Some other users on this forum seem to have struggled with the same issue.

I have seen the explanations that datetime.parse() expects the date in the local timezone format, but wasn't aware that the US timezones don't use seconds.

1 Answer 1

2

One option to work around the limitations with DateTime.parse() is to use JSON deserialization. It will take an ISO 8601 formatted date time.

The will give you millisecond resolution on the resulting DateTime.

E.g.

// Note the surrounding double quotes to make this valid JSON!
string s = '"2011-10-14T11:46:13.172Z"';
Datetime dt = (DateTime)JSON.deserialize(s,DateTime.class);
system.debug(dt);
system.assertEquals(13, dt.second());
system.assertEquals(172, dt.millisecond());

You must log in to answer this question.

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