[SalesForce] How to convert date string to DateTime type without GMT

I have a string = '11/6/2014 '. I want to convert it to a DateTime

DateTime dt = DateTime.parse('11/6/2014');

this causes an error, so I run the code below and it does work:

DateTime dt = DateTime.parse('11/6/2014 12:00 AM');

What i get is '11/6/2014 7:00 AM' – so I am assuming that it takes the time zone into account when parsing.

I want it to convert to '11/6/2014 12:00 AM' – e.g. to ignore any time zone issues.

How do I do this?
Bonus question: Is there a way to convert a time string, '11:15 AM' into a DateTime variable that stores it as '1/1/1900 11:15 AM' (date doesn't matter – but I want to preserve time as presented).

Best Answer

Looks like DateTime.parse() takes the string and parses it based on the context users locale to produce a GMT date/time in the database. DateTime.format(String) should create an output string in based on the timezone of the current context user.

So you should be able to use when data comes in:

DateTime dt = DateTime.parse('11/6/2014 12:00 AM');

Then when you go to call the web service you'd use something like:

String dtOut = dt.format('d/M/y'); -- Or M/d/y if you're using American dates.

And for the bonus, you could just use a different argument in the format function such as:

String timOut = dt.format('h:m a');
Related Topic