June 28, 2018

How to get current datetime as per the logged in user timezone in apex?

Returns the current Datetime based on a GMT calendar. So, system.now() and Datetime.now() returns Datetime based  on a GMT calendar.

Returns the current Datetime based on the user timezone settings in the user detail.

Sample code:

system.debug('System DateTime is ' + DateTime.now());

system.debug('System DateTime is ' + System.now());

system.debug('User DateTime is ' + DateTime.now().format());

system.debug('User DateTime is ' + System.now().format());


No comments:

Post a Comment