Find your content:

Search form

You are here

Datetime input field cast to another timezone

 
Share

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


Attribution to: jordan.baucke

Possible Suggestion/Solution #1

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

Attribution to: Sourabh Coolkarni

Possible Suggestion/Solution #2

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

Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #3

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


Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #4

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


Attribution to: techtrekker
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/2161

My Block Status

My Block Content