The following code returns the time of the current date:
System.debug('Current date: ' + Date.today().toStartOfMonth());
Output:
2018-08-01 00:00:00
How to remove time from date?
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityonly way I found is to keep the date format and remove the 00 from end
System.debug('Current date: ' + String.valueOf(Date.today()).removeEnd(' 00:00:00'));
Using the format method solve the issue.
System.debug('Current date: ' + Date.today().toStartOfMonth().format());
Output:
1-8-2018
Date.format()
method returns the Date as a string using the locale of the context user
or if you need it in a specific format regardless User locale use something like Datetime.format('yyyy-MM-dd')
or Datetime.format('yyyy-MM-dd', 'America/New_York')
e.g.
Date d = Date.today().toStartOfMonth();
System.debug( Datetime.newInstance(d.year(), d.month(), d.day()).format('yyy-MM-dd') );
A more elegant way I found was:
System.debug('Current date: ' + String.valueOf(Date.today().toStartOfMonth()).split(' ')[0]);
String.valueOf(Date.today()).removeEnd(' 00:00:00')
. :)