Last active
November 2, 2022 16:04
-
-
Save cemerson/fda71148b5bbe5627a5dd6bb96164e2d to your computer and use it in GitHub Desktop.
Apex Date to DateTime vice-versa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Apex DateTime/Date Stuff */ | |
// 1. Convert Datetime to Date | |
DateTime dT = System.now(); | |
Date d = Date.newInstance(dT.year(), dT.month(), dT.day()); | |
// 2. Convert Date to Datetime | |
Date d = Date.today(); | |
Datetime dt = d; | |
// 3. String date to T/Z date string | |
string startDate = '01/05/2024'; | |
startDate = startDate.replace('-','/'); // date parse can't handle the hyphens :( | |
Date dStart = Date.parse(startDate); | |
DateTime dtDate = dStart; | |
// format to TZ string | |
// NOTE: addHours() used here because otherwise the date will be reduced by 1 | https://salesforce.stackexchange.com/a/218059/43223 | |
string startDateTZString = dtDate.addHours(8).format('yyyy-MM-dd\'T\'HH:mm:ss\'Z\''); | |
system.debug(startDateTZString); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment