Technical skill practice
JavaScript Date.parse: Use Explicit ISO Instants and Check Failure
Date.parse returns a millisecond timestamp or NaN when parsing fails. Prefer an explicitly supported date-time format with a stated offset when you mean an instant. Nonstandard strings can create runtime-dependent expectations.
The task
Compare two spellings of the same instant, reject an unmistakably invalid string and inspect a date-only input separately. Avoid relying on the host’s local time zone.
Runnable Node 22 example
const assert = require('node:assert/strict');
const utc = Date.parse('2026-10-20T09:00:00Z');
const offset = Date.parse('2026-10-20T11:00:00+02:00');
assert.equal(utc, offset);
assert.equal(new Date(utc).toISOString(), '2026-10-20T09:00:00.000Z');
assert.equal(Number.isNaN(Date.parse('not-a-date')), true);
const dateOnly = Date.parse('2026-10-20');
assert.equal(new Date(dateOnly).toISOString(), '2026-10-20T00:00:00.000Z');
console.log(new Date(utc).toISOString());
console.log(new Date(dateOnly).toISOString());
Expected output and explanation
The +02:00 spelling is two hours ahead of UTC on its displayed clock, so eleven there equals nine UTC. Both parse to the same instant. The invalid text returns NaN, which should be checked before formatting or subtracting timestamps.
The date-only form in this supported grammar is interpreted as UTC midnight. That is not proof that the user intended midnight in their local zone. An appointment date and an instant are different data requirements; choose a representation and zone policy that reflects the task.
Output from the example
2026-10-20T09:00:00.000Z
2026-10-20T00:00:00.000Z
Pitfall to check
Date.parse is not a comprehensive calendar-field validator. Some out-of-range-looking inputs can normalize or vary outside the supported format. Validate required date components deliberately rather than using one successful parse as proof of every business rule.
Questions, answered
Can I omit the offset from an appointment timestamp?
Only with a clear local-time contract. A date-time without an offset can depend on the runtime’s local zone, so it is unsuitable for this host-independent instant comparison.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.