Activity Forums Salesforce® Discussions Conversion of String to DateTime format in Salesforce Org

  • Conversion of String to DateTime format in Salesforce Org

     sushant updated 5 years ago 2 Members · 2 Posts
  • Kumar

    Member
    January 4, 2017 at 1:37 pm

    Hi everyone,

    I need to convert a string that I am getting from JSON response (For eg: 2014-08-26T17:50:20.000-0700) to this format '6/9/2014 11:57' to store in field which is of text format in my salesforce org.

    Also per my requirement the field which i store date should not be in date/time format. Any guidance is appreciated.

    Thanks

  • sushant

    Member
    January 10, 2017 at 1:57 pm

    Hi Kumar,

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

    Hope this helps you

    Thanks

Viewing 1 - 2 of 2 posts

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos