2

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).

7
  • How are you getting parse to work for this date? system.debug(datetime.parse('11/6/2014')); returns an Invalid Date/time error when I try running this in developer console. If you are you some reason able to parse your date, then this should work (but it is a bit of a hack):Date dt2 = datetime.valueOf(date.newinstance(dt.year(),dt.month(),dt.day()));
    – Ben Naylor
    Commented May 18, 2014 at 22:50
  • @BenNaylor Sorry, the string is '11/6/2014 12:00 AM'. I updated the question. Also your suggestion wouldn't work if I east of the GMT line because it would go to the previous date. Commented May 19, 2014 at 14:45
  • What are you trying to achieve? Salesforce stores all dates in GMT and then converts them to the user's timezone when they are displayed to the user. By doing the conversion you proposed (i.e. setting the time to midnight) you are actually CREATING timezone issues, not solving them. You should ideally be doing any date calculations you need to do in GMT and then relying on Salesforces to displayed the localised version to your users. Commented May 20, 2014 at 9:50
  • I am trying to call an external web service that requires my time, not what Salesforce wants to store it as. Commented May 20, 2014 at 16:17
  • So just to get some clarification, are you calling your Apex code via a webservice call of some variety, and you want it to use the date that you supplied?
    – Ben Naylor
    Commented May 20, 2014 at 21:04

2 Answers 2

4

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');
4

The way I avoid tz issues is to use the format method and specify UTC as the tz. Like this:

  DateTime dt = DateTime.parse('11/6/2014 12:00 AM');
  String utcDt = dt.format('dd-MM-yyyy', 'UTC');

The above will give utcDt as '06-11-2014'

To be clear, I employ this approach when reading a record with a date field from one of my custom objects. The user specified a date which was then saved and all I need is the date as he entered it stuck into a variable where I can use it. I don't want the system messing with the value using tz considerations. In these circumstances I find that using UTC in format() avoids having hours and even days added to or subtracted from the date you're dealing with, which it would otherwise do depending on where in the world your system thinks you are.

You must log in to answer this question.

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