Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
Receive a string and verify if it is a valid date with date-fns

I just want to validate if the string that i received is a valid date using date-fns. I gonna receive a date like this '29/10/1989', BR date. I want to transform it into 29-10-1989 and check if its valid. Anyone can help me with this?

const parsedDate = parseISO('29/10/1989')

this code above doesnt work, i got 'Invalid Date'

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Try parse with locale options like this:

import { parse, isValid, format } from 'date-fns';
import { enGB } from 'date-fns/locale';

const parsedDate = parse('29/10/1989', 'P', new Date(), { locale: enGB });
const isValidDate = isValid(parsedDate);
const formattedDate = format(parsedDate, 'dd-MM-yyyy');
over 4 years ago · Santiago Trujillo Report

0

I came up with this (based on Anatoly answer):

import { parse, isValid, format } from 'date-fns';
import { enGB } from 'date-fns/locale';

function isValidDate(day, month, year) {
  const parsed = parse(`${day}/${month}/${year}`, 'P', new Date(), { locale: enGB })
  return isValid(parsed)
}

console.log(isValidDate('31', '04', '2021')); // invalid: april has only 30 days
console.log(isValidDate('30', '04', '2021')); // valid
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!