Find your content:

Search form

You are here

How to compare 2 dates in JavaScript on a Visualforce page?

 
Share

I have a Visualforce page in which I try to compare 2 dates.

The code:

<apex:page StandardController="Account">
<script>
window.document.onload = new function(e)
{
  alert("{!Account.Support_End_Date__c}");
  alert("{!NOW()}");

  if({!Account.Support_End_Date__c} <= {!NOW()})
  {
    alert("Customer is NOT under Support.");
  }
}
</script>

</apex:page>

Is not working correctly.

I want the pop up to display if the support_end_date is smaller than today/now.

UPDATE

by changing my code to the following (sorry for duplication, I think it may help others)

  if("{!Account.Support_End_Date__c}" <= "{!NOW()}")
  {
    alert("Customer is NOT under Support \nor not date is given.");
  }

I get the script to complete successfully, however, the behavior is not as requested - both dates are returned as formatted strings. How do I compare them?


Attribution to: Saariko

Possible Suggestion/Solution #1

<apex:page StandardController="Account">
<script>
window.document.onload = new function(e)
{
    var tempdate=new Date('{!Account.Support_End_Date__c}');
    var TodayDate=new Date('{!Today()}');

    if(tempdate < TodayDate){
        alert("Customer is NOT under Support.");
    }
}
</script>

</apex:page>

Attribution to: Subba Reddy

Possible Suggestion/Solution #2

I realize that this is an old question, but maybe this answer will help someone who finds it in a search.

Note that your comparison is done by the Javascript:

if({!Account.Support_End_Date__c} <= {!NOW()})

You could do the comparison in Visualforce by using a single set of braces {!...}.

if({!Account.Support_End_Date__c <= NOW()})

That should evaluate to either if(true) or if(false).


Attribution to: tomlogic

Possible Suggestion/Solution #3

The javascript is trying to compare the dates as strings. You should convert Account.Support_End_Date__c to a javascript Date object:

var tempdate=new Date('{!Account.Support_End_Date__c}');

if(tempdate < Date.now()){
    //Do Stuff
}

This should give you

<apex:page StandardController="Account">
<script>
window.document.onload = new function(e)
{
    var tempdate=new Date('{!Account.Support_End_Date__c}');

    if(tempdate < Date.now()){
        alert("Customer is NOT under Support.");
    }
}
</script>

</apex:page>

Attribution to: Jon Hazan

Possible Suggestion/Solution #4

I would go a different route. Create a simple Apex controller. Write a simple getter such as public Boolean isSupportEndDateToday { get{ return acct.Support_End_Date__c == Date.today(); } } and then reference that boolean in your JavaScript.

The whole point of MVC is to retain your controlling logic in your controller and not in the view. This also prevents anything that may happen across funky browser differences.


Attribution to: James Loghry
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/497

My Block Status

My Block Content