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
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityThe 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
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
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());
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);
}
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