Thursday 20 August 2015

Comparing date in frappe or js

I want to check given date1 is Today or not
I used 
var date2 =get_today();
var result = (date1 == date2); 
It works successfully.


After that, I want to check, given date1 is Today or Yesterday or The day before yesterday. I used <= operator, but its not working properly.
After searching I come up with this solution, before comparing date we need to parse it.
Solution is:

var a = Date.parse(dataContext.ETD);
var b =  Date.parse(get_today());
var d = (a - b)/(1000 * 3600 * 24);
var c = ((d==0)||(d==(-1))||(d==(-2))); //returns true if ETD is today, yesterday or the day before yesterday
 
Please write comment if you have any doubt or suggestion. 

2 comments:

  1. It works! I had the same problem ...this is a great solution! Thanks!

    ReplyDelete