+ Start a Discussion

Create a Dateimte in a given User's timezone

A need to create a Datetime value in a given user's timezone.  There are method for the current user and methods for GMT but no method for the current user.


Anyone know of a convenient way to do this.  I can get there timezone from the user record.  It will be a string like "America/New_York".  I supposed a could build a table with all the offsets, look up the offset, then construct a datetime in GMT and add the appropriate number of hours.


Anyone know of a better way to do this?





If you are trying to post to a vf page just output the field and it will render itself in the correct timezone.



<apex:outputfield value="{!Contact.createddate}"/>



Ken KoellnerKen Koellner

Nope, That is creating date in user's local time zone. 


I want to create date in any time zone.  Code running in system mode, want to be able to pick a user and create 9:00AM in thier timezone.


I figure I could do it by taken all the time zone codes and putting it in a Custom Setting with offset.  Then I could look up the offset, create a GMT date, and add the appropriate hours.  That's kinda a pain.  It would be nice if DateTime had a newInstance method that took a timezone name.




Hopefully this works for you...simply pass in the users timezone as you mentioned before.



DateTime utcTime = datetime.now();
string dt = utcTime.format('h:mm a','Europe/London');








Ken KoellnerKen Koellner



DateTime utcTime = datetime.now();
string dt = utcTime.format('h:mm a','Europe/London');

 Okay, now I would want to take "dt" and instanciate a datetime object and save it to a datetime field.


See, what I'm trying to do is create a datetime value for a user for whatever timezone they are in.  I have a process that's going to take a string like  "9:00AM"  and I want to for a user in America/New_York create a datetime that's UTC-5 for 9:00 AM..





Look like you just have to convert it back to a datetime type.

building on the example:



//Example Datetime
DateTime utcTime = datetime.now();

//convert to string to format datetime to local parameter
string london = utcTime.format('yyyy-MM-dd HH:mm:ss','Europe/London');
string ny = utcTime.format('yyyy-MM-dd HH:mm:ss','America/New_York');

//convert back to datetime data type to update a date/timefield
system.debug('London: '+datetime.valueOf(london));
system.debug('New York: '+datetime.valueOf(ny));


or basically this is simple method you would call 




datetime ConvertedTime = convertToLocalTime(datetime.now(),'Europe/London');

public datetime convertToLocalTime(datetime param_time, string param_timezone){
	//convert to string to format datetime to local parameter
	string var_time = param_time.format('yyyy-MM-dd HH:mm:ss',param_timezone);
	return datetime.valueOf(var_time));



It's basically all here... http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_datetime.htm .



Good luck!


That works great when you actually have  a dateTime variable which is set to the appropriate time (your example of converting now). However, if you need to actually set the variable to a specific time, say October 25, 2011 6:00AM using 'Europe/London' then what would you use in you paramtime?