You need to sign in to do that
Don't have an account?

DateTime in local Time Zone
In my apex code, system.now() returns the current Datetime based on a GMT calendar. My aim is to get the date time value in my local time zone.Since we have day light saving, I didn't think about manipulating the time difference.
I would appreciate your suggestions.
Under personal information, I have set the time zone as "Time Zone(GMT-05:00) Eastern Daylight Time (America/New_York)".
Hi
Try the following
datetime myDateTime = datetime.now();
string mydtstring = mydatetime.format();
System.debug(mydtstring);
This is in the Apex documentation under the Datetime Methods
Hi cloud nine,
try this...
public class Utility {
public static Datetime getLocalDateTime(Datetime z)
{
Datetime l = z.Date();
l = l.addHours(z.hour());
l = l.addMinutes(z.minute());
l = l.addSeconds(z.second());
return l;
}
}
Datetime gmt = Datetime.Now();
Datetime local = Utility.getLocalDateTime(Datetime.Now());
System.Debug('GMT Time: ' + gmtNow);
System.Debug('Local Time: ' + local);
Jamsie, Shoby appreciate your time.
Here is an alternate method, straight forward.
Datetime current = System.now(); // returns date time value in GMT time zone.
Date currDate = current.date();
Date currTime = current.time();
Datetime local = datetime.newinstance(currDate,currTime); // This will return date time in my local time zone.
Hi Cloud Nine,
that's a bit neater. Funnily enough I built a method to convert from local to GMT using the same pattern...
/*-----------------------------------------------------------------------------------------------------
//getGMT(Datetime l)
// -------------------------------------
// Returns GMT datetime from the passed local datetime.
//---------------------------------------------------------------------------------------------------*/
public static Datetime getGMT(Datetime l)
{
Date d = l.dateGmt();
Time t = l.timeGmt();
return Datetime.newInstance(d,t);
}
Ironically I found out I don't need either of these if I use BusinessHours.addGmt instead of BusinessHours.add.
The joys of timezones and daylight savings. :smileyvery-happy:
How about this one-liner?:
system.now().format()
This returns the system time in the local time zone for the company, I believe.
Big warning to anyone using this code on an Authenticated Sites Visualforce page! These functions will return the results in the timezone of the current user -- the user logged in or if no one is logged in, as the special Sites Guest User. This user is set to have a GMT timezone by default. I've been trying to figure out why this wasn't working for a while and it turns out it was working, but the local timezone was GMT because I was viewing the page as the guest user. To find the guest user and change its timezone, go to Develop > Sites > Your site label > Public Access Settings > Assigned Users
Here's what I did using the new TimeZone object which returns the user's time zone offset in milliseconds.
T is a Task object.
TimeZone tz = UserInfo.getTimeZone();
DateTime localTime = T.CreatedDate.AddSeconds(tz.getOffset(T.CreatedDate)/1000);
Some of these answers are misleading if you do not understand the full stack.
You need to ask yourself what do you plan to do with the DATE / DATETIME data you have in you trigger or controller.
Is the data going into a standard field for display or a custom VF field, or being sent back to the Database?
Do you need compare datetimes against start/end of month/week/year.
There is a real danger here, if you convert to LOCAL but you do not convert back before you save.
Native salesforce fields will represent themselves in Local TIme via the UI, even though they are stored and processed in GMT.
Of course no timezone info is ever stored, just year --> second
You must know the input of your method and the output.
E.g. most answers described above input in GMT and output in LOCAL. You will need to convert back again.
IMPORTANT: do not use a convert to local twice otherwise you will be 2x your time zome out.
THINK: Input need to match output at the top level. Yes convert down to LOCAL to process but then convert back to GMT.
Please see this for a demonstration, run in your execute anon:
You will see that 2 runs through the first method cause twice the timezone adjustment.
To get the current Datetime in the local time zone, you can use the following working code:
Datetime now = Datetime.now(); Integer offset = UserInfo.getTimezone().getOffset(now); Datetime local = now.addSeconds(offset/1000);
If it helps don't forget to mark this as a best answer!!!
Thanks,
Mohammad Anis
This use case now has a fairly simple answer. As a previous answer stated, using System.now().format() will give you local time of the current user. If that user is an API call, the "local timezone" is likely to be Zulu time AKA: UTC (Universal Time Code). However, the dateTime.format function accepts a few inputs that can help you specify a format and timezone which takes daylight saving and all that jazz into account.
Put simply, if you want to get local time in a specified timezone, try the following example: ...and Bob' s your uncle!
Bonus info: if you want the timezone as well (unforntunately it's longform) you can do:
If this helped you out, mark it as best answer. Hopefully it helped some people out :)
Duncan