0

Using JavaScript to write some code. What I need to do is check to see if todays date falls in between two other dates, all without using the year. Here is what I have, but I know I am doing a bunch wrong. lol. Appreciate any help. I see a solution for PHP, but I don't know if that translates to JavaScript: Check if todays date is between two other dates

var CurrentDate = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MMM d"); if (CurrentDate > "Jun 21" && CurrentDate < "Dec 25") { do stuff}

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • convert all dates to the epoch timestamps and then check if today's date falls in the required range – qualebs Jul 06 '18 at 17:14
  • Well I started with: Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MMM d yyyy HH:mm:ss"); But didnt want to use the year. Im a total noob, so must be something else I am doing wrong. – Marc Viste Jul 06 '18 at 17:16
  • `momentjs` this library may help you. Check out [here](https://momentjs.com/docs/#/query/) – unclexo Jul 06 '18 at 17:26
  • Take care in spelling “JavaScript” to avoid Search collision with “Java”. – Basil Bourque Jul 06 '18 at 17:38
  • Im doing this inn Google apps script editor, should have mentioned that. Don't know what that changes. – Marc Viste Jul 06 '18 at 17:52

1 Answers1

0

Since your dates all have the "MMM d" format, this works:

function testDate(dateStr, lowerBound, upperBound) {
  const leapYearStr = ', 2004';
  const date = new Date(dateStr + leapYearStr);
  const lower = new Date(lowerBound + leapYearStr);
  const upper = new Date(upperBound + leapYearStr);
  return lower < date && date < upper;
}

console.log(testDate('Feb 29', 'Feb 28', 'Mar 1'))

The string ", 2004" is used to convert the "MMM d" format date into a date in the year 2004, which is a leap year (so that "Feb 29" is also supported).

cybersam
  • 63,203
  • 6
  • 53
  • 76