Salesforce | Some Date functions (String to Date and more)

| By Webner

We can convert a string variable into date datatype by using the apex date class function date.parse(String);


Date var1=date.parse(String);


String var=’12/25/2016’;

Convert var to date type by:

Date var1=date.parse(var);

Now var1 contain it as a date type.
It returns an error if the date stored in a string variable is not a valid date:

String var2=’12/21/19934’; //wrong date
Date var3=date.parse(‘var2’);

It returns an error – Invalid date ’12/21/19934’

We can find the year component of the date using date.year(); 
We can find the current date using a function System.Today(); 

Integer curYear = System.Today().year();
Integer birthYear = birthDate.year();
Integer age = curYear - BirthYear;



Leave a Reply

Your email address will not be published. Required fields are marked *