Thursday, 13 February 2014

Converting DateTime to Date in Salesforce


Problem: Convert DateTime to Date 

Approach 1:

Solution: Just create a date newInstance() and pass the year,month,day from the dateTime.

Here is the Code,

1.    DateTime dt = System.now();
2.    Date myDate = date.newinstance(dt.year(), dt.month(), dt.day());
3.    System.debug('My date is-----'+myDate);

Approach 2:

Solution: Convert DateTime to Date using Date() method.

Here is the Code,

1.    DateTime dt = System.now()
2.    Date myDate= dt.date();
3.    System.debug('My date is-----'+myDate);


1 comment:

  1. How to extract date from timestamp field thru SOQL. example 2021-01-19T08:11:48.000Z o/p should be 2021-01-19

    ReplyDelete

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...