Skip to content

Instantly share code, notes, and snippets.

@jvolkov
Last active November 22, 2022 21:37
Show Gist options
  • Save jvolkov/20bf22b7744def191d8191ab26faf747 to your computer and use it in GitHub Desktop.
Save jvolkov/20bf22b7744def191d8191ab26faf747 to your computer and use it in GitHub Desktop.
Convert date to a datetime in apex
Date today = System.today();
Integer d = today.day();
Integer mo = today.month();
Integer yr = today.year();
DateTime dt = DateTime.newInstance(yr, mo, d);
// returns 2022-11-22 08:00:00
Datetime todayWithTime = dt.addHours(12);
system.debug(' todayWithTime: ' + todayWithTime);
// returns 2022-11-22 20:00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment