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
sfadm sfadmsfadm sfadm 

How can I get my correct current local time?

I need to get the current time but to be in type DateTime.

I use:
DateTime now = System.now();
but it returns wrong time.

I execute the code in 2017-08-04 00:38:46 AM

but it returns
00:38:46:023 USER_DEBUG [138]|DEBUG|now 2017-08-03 21:38:46
How can I get my correct current local time 2017-08-04 00:38:46?

I had even tried with:
DateTime now = DateTime.parse(System.Now().format());
but I still get wrong time.
01:10:09:021 USER_DEBUG [139]|DEBUG|now 2017-08-03 22:10:00
How can I get the correct current local time?



 
Best Answer chosen by sfadm sfadm
sfadm sfadmsfadm sfadm
I was trying to get the current Datetime in the local time zone, and found the following solution:
Datetime now = Datetime.now();
Integer offset = UserInfo.getTimezone().getOffset(now);
Datetime local = now.addSeconds(offset/1000);

 

All Answers

Pramodh KumarPramodh Kumar
@sfadm sfadm 

Your code is good but to get the current DateTime format you have to use below code.
you can also change the format as you like

here is the sample code:
System.now().format('yyyy-dd-mm HH:mm:ss')

Thanks
Pramodh
Glyn Anderson 3Glyn Anderson 3
The problem is that DateTime values are always in GMT.  You can't create a DateTime that is not GMT, but you can display the time in the running User's timezone using the format method.  First, make sure that the User record has the correct timezone set.  Then use something like the code below.  Note that the "MM" is important as "MM" is the month, while "mm" is the minute.  (That's a subtle error in Pramodh's response.) . The "HH" shows the hour in 24-hour format, while "hh a" would display 12-hour format with "AM" or "PM".  For more info on the SimpleDateFormat, see: https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html

<pre>
DateTime now = System.now();
System.debug( 'now ' + now.format( 'yyyy-MM-dd HH:mm:ss' );
</pre>
sfadm sfadmsfadm sfadm
I was trying to get the current Datetime in the local time zone, and found the following solution:
Datetime now = Datetime.now();
Integer offset = UserInfo.getTimezone().getOffset(now);
Datetime local = now.addSeconds(offset/1000);

 
This was selected as the best answer
Leelakrishna MunirajLeelakrishna Muniraj
Based on user created time zone datetime will formated.
Datetime now = Datetime.now();
Integer offset = UserInfo.getTimezone().getOffset(now);
Datetime local = now.addSeconds(offset/1000);
Swarnava SahaSwarnava Saha
Hi all,
I want to use the below formula in a workflow rule:
Datetime now = Datetime.now();
Integer offset = UserInfo.getTimezone().getOffset(now);
Datetime local = now.addSeconds(offset/1000);

I am writing the formula in Workflow as:- 
now.addSeconds(
UserInfo.getTimezone().getOffset(Datetime.now())/1000
)
but it is showing some error
How can I resolve this?