function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PlatFormCloudPlatFormCloud 

Converting GMT into Locale Users Time Zone

I want to show the Start Date and End Date in user's Local time Zone. For this I have created a Visual Force Page and through getter and setter method, when I am showing IN VF Page,I am getting the time in GMT. What is the way for the the logged-in user to show the local time in Visual force Page?
 
Best Answer chosen by PlatFormCloud
CloudGeekCloudGeek
Hello There,
 
Try this:
 
String timeZone = UserInfo.getTimeZone().getID();
Datetime dateGMT=System.now();// here you can user your dates e.g. createddate
Datetime d1=Datetime.valueOf(dateGMT);
string s1=d1.format();
System.debug('@@@@@@@@@@@'+s1);
 

Note: Please mark it as solved if that helps.
 

All Answers

CloudGeekCloudGeek
Hello There,
 
Try this:
 
String timeZone = UserInfo.getTimeZone().getID();
Datetime dateGMT=System.now();// here you can user your dates e.g. createddate
Datetime d1=Datetime.valueOf(dateGMT);
string s1=d1.format();
System.debug('@@@@@@@@@@@'+s1);
 

Note: Please mark it as solved if that helps.
 
This was selected as the best answer
Bryan JamesBryan James
make the returns strings and by using (DateTime.format (http://​https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_datetime.htm#apex_System_Datetime_format_2)()) the DateTime field you are using will be converted to the logged in users locale date / time and displayed as a string. If you post some code I can give you more specific instructions if you cannot figure it out. But you can get to the documentation by clicking on the link as well. 
 
PlatFormCloudPlatFormCloud
Thanks CloudGeek- You were just bang on target. This was exactly which i was looking for.