I need to convert the string datetime into user locale date time format.

for Ex:

//Consider this is Indian Time 22nd Dec 5 PM
String dateTime = '12-22-2016 17:00:00'; 

I want to convert this as per user locale. For ex, if the user is in United States, then I need to show the locale time as per the above given string datetime like below,

String dateTime = '12-22-2016 17:00:00';

then, If I need to show for America/los_angels timezone user locale,

//which is locale time for above datetime
datetimefield__c convertedDate = 12/22/2016 3:30 AM 
  • Instead of trying to guess the timezone, my suggestion is to save the time as UTC, which is what salesforce.com does. Then in your presentation layer it will display the time as per the timezone the user is in.
    – abhi
    Dec 20, 2016 at 20:57
  • @abhi - How do we save the time as UTC. Are you talking about DateTime.valueOf() method?
    – Prince
    Dec 21, 2016 at 5:49
  • The default time that is saved is the UTC time. Are you trying to save the time from a client application?
    – abhi
    Dec 27, 2016 at 13:09

3 Answers 3


You can use DateTime methods to convert the time to user's Timezone.

String strDt = DateTime.now().format('MM/dd/yyyy HH:mm:ss', 'America/Los_Angels');
  • can you please post the code by converting from the string datetime format and assign it to date time field(which I mentioned in question)
    – Prince
    Dec 20, 2016 at 12:29
  1. Convert IST into GMT(offset +5.5hours)

    • since IST = GMT+5.5hours, so GMT = IST - 5.5hours
    • DateTime convertedGMT = ISTDtTime.addseconds(-5.5*60*60);
    • where ISTDtTime- Indian Date time
  2. Find the locale Datetime using:

    String strConvertedDate = convertedGMT.format('MM/dd/yyyy HH:mm:ss','America/New_York'); 
  • It won't be always IST DateTime and America/New_York timezone. It will change dynamically. So, how can we perform on the dynamic values
    – Prince
    Dec 29, 2016 at 7:29

In addition to the aforementioned answers, if you want to store the date on your object in a specific timezone for some reason, or if you don't want to convert the date to a different locale using Apex and this value will be used in a Lightning component, you can use the localization service method in Javascript to convert the string to the right localized date based on the users location service as seen by the browser. In your Lighting Javascript controller, create a method that does the following (along with whatever else you need it to do)...

//Passed date variable from Apex to Lightning Controller
var passedDateString = component.get("v.myPassedDateString");
//Use localizationService method to format and localize the date to the users browser setting
localizedDateString = $A.localizationService.formatDate(passedDateString, "YYYY MM DD");
//Set the string back to attribute for display
component.set("v.myLocalizedDateString", localizedDateString);

You must log in to answer this question.

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