Skip to content

Instantly share code, notes, and snippets.

@johndstein
Last active August 26, 2017 19:21
Show Gist options
  • Save johndstein/4a49e115b5ca85eaad4a4d13cd5a132e to your computer and use it in GitHub Desktop.
Save johndstein/4a49e115b5ca85eaad4a4d13cd5a132e to your computer and use it in GitHub Desktop.
Salesforce Date and Datetime anomaly
// If you create a Datetime instance from a date and then create a Date
// instance from the year, month and day of the datetime, you probably
// won't get the date instance you expect.
Datetime dt = Datetime.newInstance(2017,12,31);
System.debug('dt: ' + Date.newInstance(dt.year(),dt.month(),dt.day()));
Datetime fromdt = Date.valueOf('2017-12-31');
System.debug('date: ' + Date.valueOf('2017-12-31'));
System.debug('fromdt: ' + fromdt);
System.debug('fromdt Date: ' + Date.newInstance(fromdt.year(),fromdt.month(),fromdt.day()));
// 15:18:52.1 (2422833)|USER_DEBUG|[2]|DEBUG|dt: 2017-12-31 00:00:00
// 15:18:52.1 (2502589)|USER_DEBUG|[4]|DEBUG|date: 2017-12-31 00:00:00
// 15:18:52.1 (2547071)|USER_DEBUG|[5]|DEBUG|fromdt: 2017-12-31 00:00:00
// 15:18:52.1 (2652146)|USER_DEBUG|[6]|DEBUG|fromdt Date: 2017-12-30 00:00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment