JSFiddle - React, Tailwind, and code Playground

by Bateman

TypeScript

console.clear();

function compare(date1: Date, date2: Date) {
  return (new Date(date1.getFullYear(), date1.getMonth(), date1.getDate()) - new Date(date2.getFullYear(), date2.getMonth(), date2.getDate()));
}

function isDateInPeriod(date: Date, period: [Date, Date]): boolean {
  return compare(period[0], date) <= 0 && compare(date, period[1]) <= 0;
}

const start = new Date(2018, 5, 1)
const end = new Date(2018, 5, 6)

const period = [start, end];

const d1 = new Date(2018, 4, 3)
const d2 = new Date(2018, 6, 3)
const d3 = new Date(2018, 5, 3)
const d4 = new Date(2018, 5, 1)
const d5 = new Date(2018, 5, 6)

function expect < T > (actual: T) {
  return {
    toBe(expected: T, message: string) {
      if (actual != expected) {
        throw new Error(message || 'actual was ' + actual + ', expected was ' + expected)
      }
    }
  }
}

function it(message: string, run: () => void) {
  try {
    run();
    console.log('[success]', message)
  } catch (e) {
    console.warn('[fail]', message, ':', e.message)
  }
}

it('should not be in range', () => {
  expect(isDateInPeriod(d1, period)).toBe(false)
})

it('should not be in range', () => {
  expect(isDateInPeriod(d2, period)).toBe(false)
})

it('should be in range', () => {
  expect(isDateInPeriod(d3, period)).toBe(true)
  expect(isDateInPeriod(d4, period)).toBe(true)
  expect(isDateInPeriod(d5, period)).toBe(true)
})