-2

How do I go about checking between two dates in a current month?

For example, I want to check between the 1st of January and the 8th of January regardless of year (I'm British so my dates are (dd/mm/yyyy):

var date = new Date(); //This is the current date

date1 = new Date('01, 01, date.getFullYear()');
date2 = new Date('08, 01, date.getFullYear()');

if(date >= date1 && date <= date2) {
// Do Something
} else {
// Don't do something
}

Or another example, let's say Halloween:

var date = new Date();

date1 = new Date('31, date.getMonth(), date.getFullYear()');

if(date == date1) {
// Do Something
} else {
// Don't do something
}

I'm not quite sure how to format it in Javascript, any help would be greatly appreciated!

Ryan
  • 45
  • 2
  • 11
  • 1
    That doesn't even seem like correct syntax. – Charlie Fish Jan 01 '18 at 21:30
  • That's why I asked the question – Ryan Jan 01 '18 at 21:32
  • 1
    Not even clear what *"check between two dates"* means. Provide a proper explanation of what you are trying to accomplish – charlietfl Jan 01 '18 at 21:34
  • Done @charlietfl – Ryan Jan 01 '18 at 21:37
  • Still not very clear. What is input for this and what are expected results? See [mcve] – charlietfl Jan 01 '18 at 21:38
  • Okay, so let's say today is between the 1st of January and the 8th of January I will change the

    tag on my page to say something. I already know how to change it but don't know about date checking.

    – Ryan Jan 01 '18 at 21:40
  • Not hard to research how to get the date of month and current month from javascript date object. Then compare those values to your boundary values – charlietfl Jan 01 '18 at 21:43
  • Please familiarize yourself [with the basics](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date) before asking a question. – Derek 朕會功夫 Jan 01 '18 at 21:45
  • @charlietfl I already tried doing this for the past 30 minutes or so and I am still confused that's why I came here – Ryan Jan 01 '18 at 21:45

2 Answers2

2

You are initializing your Date objects incorrectly. If you want to initialize a date with a string, that string needs to have a format that can be processed by Date.parse.

The string you are passing, however, looks like you actually don't want to pass any strings at all. So just omit the single quotes, like this:

var date1 = new Date(date.getFullYear(), 0, 1);

Note that the first argument is the year, the second is the month (zero-based, so 0 is January) and the third is the day of the month.

Patrick Hund
  • 19,163
  • 11
  • 66
  • 95
1

A few points...

  • new Date parameters are (year, month, day) regardless of your local settings.
  • The single quotes around the parameters aren't correct syntax.
  • Months in JavaScript are zero-indexed, so January is zero.

So your date2 should be date2 = new Date(date.getFullYear(), 0, 8).

Also get in the habit of putting var in front of variable declarations. Don't worry why just yet; you'll learn about that as you go.

So your code should go something like this:

var date = new Date();

var date1 = new Date(date.getFullYear(), 0, 1);
var date2 = new Date(date.getFullYear(), 0, 8);

if(date >= date1 && date <= date2) {
    // this part was correct :)
    //Do Something
}
Ed Gibbs
  • 25,924
  • 4
  • 46
  • 69
  • I don't suppose there would be a way of making this work on iOS? I recently found out they use a different way of displaying the date on iOS – Ryan Jan 02 '18 at 22:10
  • Sorry @Ryan, I'm pretty feeble with iOS. If you're using JavaScript anywhere (including iOS) then it should be the same. If you're using Objective C then [maybe this will help](https://stackoverflow.com/a/1072993/2091410)? Good luck! – Ed Gibbs Jan 03 '18 at 03:32