DEV Community

Tom
Tom

Posted on

Validate a Warehouse Calendar Date Without Rolling It Into Another Month

A receiving plan asks for a calendar date. The input says 2026-02-31. Turning numeric components into a JavaScript date can normalize overflow instead of rejecting the requested day. A valid timestamp is therefore not sufficient evidence that the original calendar date existed.

For a Shopify 3PL import review, define a date-only contract before parsing. This example accepts exactly YYYY-MM-DD, with years 0001 through 9999, and returns the original string. It does not choose an appointment time, timezone or warehouse operating day.

Validate the calendar before creating a time

Date.UTC() adjusts out-of-range components and treats years 0 through 99 specially. Here, a small Gregorian calendar check avoids those constructor behaviors entirely.

'use strict';
const assert = require('node:assert/strict');

function calendarDate(value) {
  if (typeof value !== 'string') throw new Error('Date text required');
  const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
  if (!match || match[0] !== value) throw new Error('Use YYYY-MM-DD');
  const year = Number(match[1]);
  const month = Number(match[2]);
  const day = Number(match[3]);
  if (year < 1 || month < 1 || month > 12) {
    throw new Error('Year or month out of range');
  }
  const leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
  const lengths = [31, leap ? 29 : 28, 31, 30, 31, 30,
    31, 31, 30, 31, 30, 31];
  if (day < 1 || day > lengths[month - 1]) {
    throw new Error('Day does not exist in this month');
  }
  return value;
}

for (const value of ['0001-01-01', '0099-12-31', '2000-02-29',
  '2024-02-29', '2026-04-30', '9999-12-31']) {
  assert.equal(calendarDate(value), value);
}
for (const value of ['0000-01-01', '1900-02-29', '2100-02-29',
  '2026-02-29', '2026-02-31', '2026-04-31', '2026-00-10',
  '2026-13-01', '2026-01-00', '2026-1-01', ' 2026-01-01',
  '2026-01-01\n', '2026-01-01T00:00:00Z', '', null, 20260101]) {
  assert.throws(() => calendarDate(value));
}
console.log('Calendar-date boundary checks passed');
Enter fullscreen mode Exit fullscreen mode

Save as calendar-date.cjs and run node calendar-date.cjs with Node.js 24. The checks cover century leap-year exceptions, February overflow, month lengths, low four-digit years, whitespace, timestamps and non-text values. They verify rejection of the supplied value rather than a later normalized substitute.

A calendar date still needs an operational meaning

A drop shipping delivery plan might carry a supplier's proposed arrival date. A shopify fulfillment release plan might carry a seller's requested dispatch date. Both can use this string contract while representing different events. Name the field so a reader can distinguish a proposal from a confirmed booking or an actual completion.

In China fulfillment work, keep a date-only field separate from carrier events that identify a precise instant. Date.parse() parses strings into timestamps; that is a different responsibility from this validation helper. A China 3PL adapter must separately agree timezone, cutoff and scheduling rules when an instant is needed.

Suppose a planning brief is called “International Fulfillment for Folligen.” Validate its actual date fields against their agreed meaning; the project name supplies no schema or client history. I work with YX Fulfillment, whose Shopify handover guide provides operational context. These locally executed checks do not demonstrate a deployed warehouse connector or a live store integration.

Workstations with monitors and printers in a warehouse handling area

Warehouse workstations and equipment.

Top comments (0)