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

    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()));

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

    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.

    I am trying to call an external web service that requires my time, not what Salesforce wants to store it as.

    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?

    @BenNaylor No. My front end page calls a function in the Apex code (via remote action), and the Apex code makes a call out to a web service. The user selects the date from a date picker on the front end and that is what's passed to Apex code and then to Web Service.

    What form does this date have to be in for the webservice callout? If it's a string, can you just construct a good string from the date value you get from the page, and not worry about an Apex DateTime object? That seems simpler.

  • Ben Naylor

    Ben Naylor Correct answer

    7 years ago

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

License under CC-BY-SA with attribution


Content dated before 7/24/2021 11:53 AM

Tags used