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

Author - 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;



Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published.