Date & Time Validators

Date and time validators including age verification, business day checks, leap year detection, and timezone handling.

20 code snippets available

Is Adult (18+)

datetime

Checks if the provided date of birth makes the user at least 18 years old.

#dob#age#restriction
ts
const isAdult = (birthDate: Date): boolean => {
  const today = new Date();
  const age = today.getFullYear() - birthDate.getFullYear();
  const m = today.getMonth() - birthDate.getMonth();
  return age > 18 || (age === 18 && m >= 0 && today.getDate() >= birthDate.getDate());
};

Is Business Day

datetime

Checks if date is Mon-Fri.

#workday#weekday
ts
const isBusinessDay = (date: Date): boolean => {
  const day = date.getDay();
  return day !== 0 && day !== 6;
};

Is Weekend

datetime

Checks if date is Saturday or Sunday.

#weekend#holiday
ts
const isWeekend = (date: Date): boolean => date.getDay() === 0 || date.getDay() === 6;

Is Leap Year

datetime

Determines if a year is a leap year.

#calendar#year
ts
const isLeapYear = (year: number): boolean => (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;

Days Between Dates

datetime

Calculates number of days between two dates.

#diff#duration
ts
const daysBetween = (d1: Date, d2: Date): number => {
  const oneDay = 24 * 60 * 60 * 1000;
  return Math.round(Math.abs((d1.getTime() - d2.getTime()) / oneDay));
};

Is Future Date

datetime

Checks if a date is strictly in the future.

#validation#time
ts
const isFuture = (date: Date): boolean => date.getTime() > Date.now();

Is Valid Date Object

datetime

Checks if a Date object is valid and not "Invalid Date".

#check#error-handling
ts
const isValidDate = (d: any): boolean => d instanceof Date && !isNaN(d.getTime());

Is Past Date

datetime

Checks if a date is in the past.

#validation#time
ts
const isPast = (date: Date): boolean => date.getTime() < Date.now();

Is Same Day

datetime

Checks if two dates are on the same day.

#comparison#calendar
ts
const isSameDay = (d1: Date, d2: Date): boolean => d1.toDateString() === d2.toDateString();

Format Date

datetime

Formats date to locale string.

#date#format#locale
ts
const formatDate = (date: Date, locale = 'en-US'): string => 
  date.toLocaleDateString(locale, { year: 'numeric', month: 'long', day: 'numeric' });

Calculate Age

datetime

Calculates age from birthdate.

#age#calculation#dob
ts
const getAge = (birthDate: Date): number => {
  const today = new Date();
  let age = today.getFullYear() - birthDate.getFullYear();
  const m = today.getMonth() - birthDate.getMonth();
  if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) age--;
  return age;
};

Time Ago

datetime

Returns human-readable time difference.

#time#relative#format
ts
const timeAgo = (date: Date): string => {
  const seconds = Math.floor((Date.now() - date.getTime()) / 1000);
  const intervals = { year: 31536000, month: 2592000, week: 604800, day: 86400, hour: 3600, minute: 60 };
  for (const [unit, sec] of Object.entries(intervals)) {
    const interval = Math.floor(seconds / sec);
    if (interval >= 1) return `${interval} ${unit}${interval > 1 ? 's' : ''} ago`;
  }
  return 'just now';
};

ISO 8601 Date

datetime

YYYY-MM-DDTHH:mm:ss.sssZ

#iso#standard
ts
const isValid = (s: string): boolean => /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$/.test(s);

Date (YYYY-MM-DD)

datetime

Standard date format.

#format
ts
const isValid = (s: string): boolean => /^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/.test(s);

Time (24h)

datetime

HH:MM or HH:MM:SS

#time
ts
const isValid = (s: string): boolean => /^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$/.test(s);

Time (12h)

datetime

12-hour format with AM/PM.

#time#format
ts
const isValid = (s: string): boolean => /^(0?[1-9]|1[0-2]):[0-5][0-9]\s?(AM|PM|am|pm)$/.test(s);

Date (DD/MM/YYYY)

datetime

European date format.

#date#format
ts
const isValid = (s: string): boolean => /^(0[1-9]|[12]\d|3[01])\/(0[1-9]|1[0-2])\/\d{4}$/.test(s);

Date (MM/DD/YYYY)

datetime

US date format.

#date#format
ts
const isValid = (s: string): boolean => /^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$/.test(s);

Timezone Offset

datetime

+HH:MM or -HH:MM format.

#timezone#offset
ts
const isValid = (s: string): boolean => /^[+-](0[0-9]|1[0-4]):[0-5][0-9]$/.test(s);

Unix Timestamp

datetime

10 or 13 digit timestamp.

#timestamp#unix
ts
const isValid = (s: string): boolean => /^\d{10,13}$/.test(s);

About Date & Time Validators

Our date & time validation snippets are designed to handle the most common validation scenarios you'll encounter in modern software development. Each snippet is thoroughly tested, optimized for performance, and follows industry best practices.

All validators are available in multiple programming languages including JavaScript, TypeScript, Python, Go, PHP, C#, and Rust. Simply copy the code snippet, adapt it to your specific needs, and integrate it into your project. Every snippet is MIT licensed—free for personal and commercial use.

Browse Other Categories