how to validate future date( > today) in snippet

  • 1
  • Question
  • Updated 3 years ago
in my model i have a field like expected_start_date__c. this date should be future date. how to write snippet code for this validation. Thanks in advance
Photo of raj


  • 840 Points 500 badge 2x thumb

Posted 3 years ago

  • 1
Photo of David Giger

David Giger

  • 1,758 Points 1k badge 2x thumb
Hi raj

This is pretty simple. You just need to compare the two dates using common operators:
var today = new Date(),	    
    tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
console.log(today > tomorrow); //false
console.log(today < tomorrow); //true
console.log(today == tomorrow); //false
Try it out and play with it. Alter the script to fit your needs and you should be good to go.

Photo of raj


  • 840 Points 500 badge 2x thumb
my requirement for that field is , if that field is null or undefined or past date, then i want throw error to add the future date in if condition

if (Startdate === '' || Startdate === null || Startdate === undefined ) {

            message: 'Please Enter Expected start date and Expected date should be future date',
            severity: 'ERROR'

    return false;
Photo of Matt Sones

Matt Sones, Champion

  • 32,106 Points 20k badge 2x thumb
raj, you need to get your salesforce date into javascript and then compare it to a new Date().
take a look at