So here is the first one.
var aDate = new Date(); // will give current date
To get a specific date, you can provide year, month and day.
var anotherDate = new Date(yyyy,mm,dd);
Take care of the fact that months are from 0-11 i.e. January is 0 and December 11.
Hence, new Date(2008,10,09) is 9th November 2008 not 9th October.
Below is the code
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds var firstDate = new Date(2008,01,12); var secondDate = new Date(2008,01,22); var diffDays = Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));
What we did is
- we took two dates
- got milliseconds for each using getTime() function
- calculated their difference
- and finally divided the difference with the number of milliseconds in one day
Simple, isn’t it???