7

I have some friends' birthdays and want to separate them as follows :

  • birthdays which fall within the current week (within remaining days of current week starting from current day).
  • birthdays which fall within the current month (within remaining days of current month starting from current day).
  • birthdays which fall within the next month.

So all I want to know how to test each date in javascript to see if it falls within the remaining days of the current week/current month/next month.

N.B: say I have those dates in m/d/Y(06/29/1990) format.

Thanks

Beetroot-Beetroot
  • 18,022
  • 3
  • 37
  • 44
flyleaf
  • 985
  • 4
  • 11
  • 27
  • 1
    [Parse the date](http://stackoverflow.com/questions/1576753/parse-datetime-string-in-javascript) then use the [accessors](http://www.quackit.com/javascript/javascript_date_and_time_functions.cfm) of the Date object and compare the desired fields. – Denys Séguret Jul 09 '12 at 09:23

2 Answers2

8

Convert your date and current time to Date object and use it for comparison. Some dry coding:

var now = new Date()
if (
   (check.getFullYear() == now.getFullYear()) &&
   (check.getMonth() == now.getMonth()) &&
   (check.getDate() >= now.getDate())
) {
   // remanining days in current month and today. Use > if you don't need today.
}

var nextMonth = now.getMonth() + 1
var nextYear = now.getFullYear()
if (nextMonth == 12) {
   nextMonth = 0
   nextYear++
}
if (
   (check.getFullYear() == nextYear) &&
   (check.getMonth() == nextMonth)
) {
   // any day in next month. Doesn't include current month remaining days.
}

var now = new Date()
now.setHours(12)
now.setMinutes(0)
now.setSeconds(0)
now.setMilliseconds(0)
var end_of_week = new Date(now.getTime() + (6 - now.getDay()) * 24*60*60*1000 )
end_of_week.setHours(23)
end_of_week.setMinutes(59)
end_of_week.setSeconds(59) // gee, bye-bye leap second
if ( check >=now && check <= end_of_week) {
   // between now and end of week
}
Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68
  • any way to check if the checking date falls within remaining days of current week? – flyleaf Jul 09 '12 at 10:28
  • Yeah. Get current time, add difference between max day of week (6) and current day and use this new end of week time for check. – Oleg V. Volkov Jul 09 '12 at 10:29
  • I think you need to use this for determining the start date of the week: ` var date = new Date(); var now = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay()); ` – Keith Holliday Nov 17 '16 at 16:36
2

the code Using the Parse Date is

var selecteddate = '07/29/1990';
var datestr = selecteddate.split('/');

var month = datestr[0];
var day = datestr[1]; 
var year = datestr[2];

var currentdate = new Date();
var cur_month = currentdate.getMonth() + 1;
var cur_day =currentdate.getDate();
var cur_year =currentdate.getFullYear();

if(cur_month==month && day >= cur_day)
{
 alert("in this month");
}

   else
   {
  alert("not in this month");
   }    ​
muthu
  • 5,381
  • 2
  • 33
  • 41