+ Start a Discussion
satyamsatyam 

Please help to resolve my date comparisiion problem

Hi,

 

I have requirement that in javascript inside if condition i want to compare two date field and i am compairing this dates for that i am using below codes

 

var created='{!student__c.CreatedDate}';
if(created>'01/06/2012')
{
alert('All data is already present here');
}
else
{
window.open('http://www.google.co.in');
}

 

But its not working so how i can resolve this issue.

 

Thanks :))))

jhansisridhar_2011jhansisridhar_2011

Hi Satyam,

Date always should be in this formate "MM/DD/YYYY",

 

var created='{!student__c.CreatedDate}';
if(created > '06/01/2012')
{
alert('All data is already present here');
}
else
{
window.open('http://www.google.co.in');
}

 

Mark as solution this is post Helps u Out.

satyamsatyam

Hi,

 

Here if condition is not checking and comparision operator is not workign.

 

And created date is date time field and comparing with date field .

 

What can be solution for this.

 

 

Thanks:))))

SamReadySamReady

Satyam,

 

JavaScript has a Date object:

var date1 = new Date(2012, 5, 1); // creates a new Date for the June 1, 2012

var date2 = new Date(); // creates a new Date representing today

 

 

The easiest way to convert the date format would be to do some Apex logic in a custom controller where you manipulate the createdDate to put it in this format... This can be done using the standard Apex Date methods (which you can find in the Apex Reference Guides). In your Apex class, it would be something along the lines of: 

 

Date myYear = Student__c.CreatedDate.year(); //these would have to be public and exposed to your page

Date myMonth = Student__c.CreatedDate.month(); 

Date myDay = Student__c.CreatedDate.day(); 

 

Then (referencing the above line of code):

var date2 = new Date({!myYear}, {!myMonth}, {!myDay}); // creates a new Date representing CreatedDate


To find the difference, convert both dates to their millisecond representations and subtract:

var conv_date1 = date1.getTime();

var conv_date2 = date2.getTime();

var diff = conv_date1 - conv_date2;

 

If date1 is before date2, the diff will be negative. So in your case, you can make a conditional where

if(diff > 0)

{ .... logic }

 

There are probably conversion methods out there for parsing dates in JavaScript...the Apex ones are just much easier in my opinion :)