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

559
Views
RangeError: locale must contain formatDistance property when using locale variable in Next.js with date-fns

I have a website with multiple languages done in Next.js. Next.js provides several methods to get all the locales, here are the following:

{locale} // current locale in use 'es'
{locales} // all the configured locales in an array [ "en", "de", "es", "ja", "ru" ]
{defaultLocale} // if no locale provided, will use en

Then in my code, i have this date-fns method to get the distance in days for a given date i get from my createdAt value.

// my import languages
import { es, de, ja, en, ru } from 'date-fns/locale';


<BodyOne>
  This category was funded{' '}
  {formatDistance(new Date(category.createdAt), new Date(), {
    locale: locale,
    })}
</BodyOne>

This gives me the following error:

RangeError: locale must contain formatDistance property

I don't understand why, this works:

{
  locale: es,
   })}

But this one with the variable locale doesn't.

{
  locale: locale,
   })}

if I create a new constant, let's say:

const myCurrentLang = es;

it works, but if I output:

const myCurrentLang = 'es';

I get the error: RangeError: locale must contain formatDistance property

Maybe i need to convert the 'es' value to other type in order to make date-fns work.

about 4 years ago · Juan Pablo Isaza
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!