+ Start a Discussion
David PicksleyDavid Picksley 

String ('yyyy-MM-dd') to date using date.parse returns ('yyyy-MM-dd hh:MM:ss')

Hi All,

I am trying to output a date type of yyyy-MM-dd, not as a string. I can create the correctly formatted string and insert that into a set.
currentDate = Date.today().toStartofMonth(); // Outputs 2018-12-01 00:00:00

firstDayOfCurrentMonth = currentDate.addMonths(i).toStartofMonth();

String currentDateString = String.valueOf(firstDayOfCurrentMonth).removeEnd(' 00:00:00'); // Outputs 2018-12-01 as a string

However, the issue is converting that string back into the Date type using either .valueOf or .parse
// Outputs 2018-12-01 00:00:00

Date.valueOf(currentDateString.removeEnd(' 00:00:00'))
// Outputs 2018-12-01 00:00:00

// Throws error with Invalid Date : 2018-12-01

My question is, how can I convert the correctly formatted string back into the Date type?

Many thanks!!
Best Answer chosen by David Picksley
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi David

Try using Date.valueOf method.Please find below a piece of code to check :

 system.debug('Date : +  Date.ValueOf('2018-12-01 14:05:07'));