4

Is there a way to cast the input of a timedate field to another timezone's GMT...?

If the user is in say: MDT, an input of 12:00, will be saved as 18:00, but if I want that value to be in EDT, I need the value to be saved as 16:00GMT

  • Can you clarify what you need to do with the manipulated date? i.e. date arithmetic, display formatting, etc. – jkraybill Oct 10 '12 at 22:04
  • I'm not sure I fully follow what is required. I can say that you should always store DateTimes in GMT (UTC +0). Then adjust them to the required timezone for display only. My understanding is Salesforce works this way. Are you looking for a way to show the current user a DateTime value in a timezone other than their current one? – Daniel Ballinger Oct 10 '12 at 23:40
  • I need to set the users input from a Datetime input field to a target timezone they specified from the list. Since the input is automatically converted to the users default timezone UTC, changing it to a specified UTC required a known offset + daylight savings time modification. It took me most of the afternoon but I wrote a mapping for all of America/Europe. I'll post the source somewhere here. – jordan.baucke Oct 11 '12 at 1:38
5

The datetime in salesforce is always stored in GMT (UTC). The time is corrected to the users time zone when displaying it on the UI.

Datetime GMTDate = 
  Datetime.newInstanceGmt(2011,6,1,12,1,5);
String strConvertedDate = 
  GMTDate.format('MM/dd/yyyy HH:mm:ss', 
                 'America/New_York');
// Date is converted to the new time zone and is //adjusted for daylight saving time. 

Valid time zone values for the timezone argument are the time zones of the Java TimeZone class that correspond to the time zones returned by the TimeZone.getAvailableIDs method in java

4
DateTime currenttime = DateTime.now();
currenttime.format('h:mm a', 'GMT+01:00');

Follwoing will help you in formating as well.

Identify the local of the user from Userinfo class and according to his local you can maintain a custom setting and write a utility to add as shown above.I have used system.now you can input your data in date time format

Hope it helps

4

With the launch of 27.0 version salesforce has released timezone formatting methods .

TimeZone tz = UserInfo.getTimeZone();
System.debug('Display name: ' + tz.getDisplayName());
System.debug('ID: ' + tz.getID());
// During daylight saving time for the America/Los_Angeles time zone
System.debug('Offset: ' + tz.getOffset(DateTime.newInstance(2012,10,23,12,0,0)));
// Not during daylight saving time for the America/Los_Angeles time zone
System.debug('Offset: ' + tz.getOffset(DateTime.newInstance(2012,11,23,12,0,0)));
System.debug('String format: ' + tz.toString());
0
public void DateTimeConversion(integer y, integer d,integer m, integer h,integer min,integer s)
    {
        Datetime GMTDate = 
        Datetime.newInstanceGmt(y,m,d,h,min,s);
        System.debug('GMTDate '+GMTDate);
        String strConvertedDate = GMTDate.format('MM/dd/yyyy HH:mm:ss', 'America/New_York');
        System.debug('strConvertedDate'+strConvertedDate);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.