19

Is there any difference between System.today() and Date.today()?

Similarly, is there any difference between System.now() and DateTime.now()?

2 Answers 2

29

They give you the same data. Their descriptions are almost verbatim as well:

System.now()
Returns the current date and time in the GMT time zone.

Datetime.now()
Returns the current Datetime based on a GMT calendar.

And for today they are indeed verbatim:

System.today()
Returns the current date in the current user's time zone.

Date.today()
Returns the current date in the current user's time zone.

If you care about character count, Date.today() is more efficient than using System, but System.now() is more efficient than using Datetime. Either way, it's a two character difference.


There is actually a performance difference as well. Note that even slower executions will take less than 5 microseconds, so this difference is not likely to affect your code performance on the whole unless you are calling these methods millions of times.

  • For Date.today(), the cost per invocation is about 4.43µs, whereas System.today() costs about 3.85µs per invocation, a difference of about ~15%.
  • For Datetime.now(), the cost per invocation is about 3.48µs, whereas System.now() costs about 2.97µs per invocation, a difference of ~17%.
14

Historically, only the System methods were originally available (System.now and System.today). The newer methods were added later, presumably because people felt it was logical that you'd find the current date in a class called Date, and the current time in a class called DateTime. They are exactly identical in behavior, so feel free to use whichever you prefer.

3
  • 1
    +1 for the historical perspective. Do you know what API Version saw these changes?
    – Adrian Larson
    Nov 2, 2016 at 15:38
  • 2
    @AdrianLarson I can't seem to find the exact version, but if I had to guess it was somewhere between 24 and 27, as it has been a few years. I recall reading about the new methods and wondering if they'd removing the System methods, which, of course, they never did. Interestingly, those methods still compile when changing the API version to 10.0, which means it's one of those things that they didn't version in the compiler.
    – sfdcfox
    Nov 2, 2016 at 16:15
  • 1
    Definitely 24 or earlier. It's been around since I started working on the platform, and I think that's the first one I worked on.
    – Adrian Larson
    Nov 2, 2016 at 22:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .