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

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

now().format()
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());

Output:


No comments:

Post a Comment