Skip to content

Instantly share code, notes, and snippets.

@lukethacoder
Created January 29, 2021 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukethacoder/a4d35f456b7675bb80930530fea67f65 to your computer and use it in GitHub Desktop.
Save lukethacoder/a4d35f456b7675bb80930530fea67f65 to your computer and use it in GitHub Desktop.
APEX: Get time between two Datetime instances
Long dt1Long = DateTime.now().addDays(-1).getTime();
Long dt2Long = DateTime.now().getTime();
Long milliseconds = dt2Long - dt1Long;
Long seconds = milliseconds / 1000;
Long minutes = seconds / 60;
Long hours = minutes / 60;
Long days = hours / 24;
@lukethacoder
Copy link
Author

could expand to calculate days, months and years too if needed.

if only needing days, use DateTime.daysBetween() instead.

(DateTime.now().addDays(-1)).daysBetween(DateTime.now());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment