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?

  • 2
    You're asking the wrong question. What are you trying to do?
    – sfdcfox
    Aug 3, 2017 at 22:20
  • I think I've explained it quite clearly. I'm trying to get my local my correct current local time. Apparently it is not working with System.now(); because it is giving me wrong seconds, minutes, hours and days.
    – sfdev
    Aug 3, 2017 at 22:23
  • Supplications as to the urgency of your requirement are off topic and also quite rude.
    – Adrian Larson
    Aug 3, 2017 at 22:28
  • I am trying to get the correct current time including all components year, month, day hour, minutes and seconds. Because all components are included in DateTime that is why I need DateTime object.
    – sfdev
    Aug 3, 2017 at 22:28

4 Answers 4


This code will get you the local time for the logged in user, by leveraging the timezone offset to subtract/add from GMT. It's even smart enough to account for Daylight Saving.

TimeZone tz = UserInfo.getTimeZone();
DateTime dt = Datetime.now();

system.debug('Actual Time as String ' + dt.format());
system.debug('Offset ' + tz.getOffset(dt)/1000);
system.debug('Formatted Time ' + dt.addSeconds((tz.getOffset(dt)/1000)));

If you want to always convert to a specific timezone (e.g.: EST) then substitute the first line for:

Timezone tz = Timezone.getTimeZone('America/New_York');


DateTime values are represented by an epoch time in GMT, and converted to the appropriate date/time when requested via the appropriate methods. System.debug, coincidentally, always displays GMT. Try this:

DateTime now = DateTime.now();
Integer hours = now.hour(), minutes = now.minute(), seconds = now.second();

In my org, I ran it just a few seconds ago, and came up with the following values:

17:21:03.1 (1425986)|USER_DEBUG|[3]|DEBUG|2017-08-03 23:21:03

17:21:03.1 (1452095)|USER_DEBUG|[4]|DEBUG|17:21:3

As you can see, it's 21 minutes past 5 PM in my local time zone. System.debug shows the GMT value, and by calling hour(), minute(), and second(), I received the local time.

Per my original comment, whatever you think is going on isn't what's going on. DateTime values are always in GMT, and accommodate themselves for you as necessary. I recommend that you consider asking about whatever was originally stopping you before, because it's most likely a misunderstanding about how DateTime works.

If you need to adjust to another time zone, do feel free to use the TimeZone class, but be aware that using a time altered by addSeconds(tz.getOffset()/1000) may return incorrect results if you're querying the database, etc. The TimeZone class is typically used to convert a user's input from a local time zone into GMT, since the entire platform runs on GMT.

  • I was banging my head from past 7 hours why system.debug() returns wrong results for a different timezone logged-in user. Salesforce documentation needs to be more clearer. Thank you!
    – cartman
    Feb 9, 2018 at 2:44
  • Simply Awesome @sfdcfox
    – bharath
    Apr 26 at 9:11

DateTime usually shows the time in GMT.

Format function retrieves the time based on user's timezone defined in Salesforce.

Let's say, this timezone is CST. So, format() or formatLong() (Converts the date to the local time zone and returns the converted date in long date format.) returns CST time.

System.debug('current time=' + System.Now().format());

Now if you are working at EST timezone. Salesforce never returns this EST time. You need to put a logic behind that.

  • System.Now().format(); is giving me String. I need Datetime. Could you please advise how to get Datetime instead of String?
    – sfdev
    Aug 3, 2017 at 22:05
  • string x=System.Now().format(); dateTime y=dateTime.parse(x); Aug 3, 2017 at 22:40

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);
  • 6
    Interesting. You ignored all of the answers above and posted (and accepted) your own identical answer instead. Not exactly a typical use of this community, for sure. Aug 7, 2017 at 19:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.