Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using a datepicker to select dates (month and year only). I am using Angular JS too. When the data is bound its in format 2014-08-12T13:09:00 and when I select any from date picker its in format Fri Aug 1 18:52:21 UTC+0530 2014

How can I compare these two dates with month and year only.

Is it possible to convert to JSON string into format that has month and year only and then compare???


Please help me
Posted
Updated 11-Aug-14 21:54pm
v3

One way to compare client-side would be to make a date object from each valid string, get the month/year and compare, similar to this:
JavaScript
var date1 = new Date('Fri Aug 1 18:52:21 UTC+0530 2014');
var date2 = new Date('2014-08-12T13:09:00');

// gets month and year values - month is zero based so +1 ensures correct month, though if you simply want to compare values you don't need to do +1 nor the `/`! - see details below
var date1Short = date1.getMonth() + 1 + '/' + date1.getFullYear(); // date1Short is now '8/2014'
var date2Short = date2.getMonth() + 1 + '/' + date2.getFullYear(); // date2Short is now '8/2014'

// Now compare your values
if(date1Short === date2Short){
  //....
} else {
   //....
}

DEMO - Comparing month and year values


Alternative Formatting
JavaScript
// if values are only used for compare then no +1 or '/' is needed and the below will work too
// + '' is needed below to prevent addition (7+2014) and ensure string concatenation instead (72014)

var date1Short = date1.getMonth() + '' + date1.getFullYear(); // date1Short would then be '72014';
var date2Short = date2.getMonth() + '' + date2.getFullYear(); // date2Short would then be '72014';

If you need to compare server side, use the same getMonth()/getFullYear()... methods to pass the values to the server.
 
Share this answer
 
v3
Hi,

Thanks for the response. I created a JSOn date and compared it
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900