Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

128
Vistas
Localize date-fns

I had a working solution for date-fns using the following:

const formatMonth = format("MMM");

Then I imported the locale and tried the following:

import { de } from 'date-fns/locale';

const formatMonth = format("MMM", {locale: de});

Getting the error: RangeError: Invalid time value. How do I change the language to german? The docs kinda suggest exactly the same, but it's not working.

EDIT: Maybe that is relevant and showcases, why the new Date() is not necessary for the format:

const months = eachMonthOfInterval({
    start: startOfYear(value),
    end: endOfYear(value)
});
{months.map((month) => (
   <ListboxOption
     key={month.toDateString()}
     value={month}
     date={formatMonth(month)}
     disabled={!isOlderThen18Years(month)}
   ></ListboxOption>
))}

Also, this code works in code sandbox, but won't in my react application. Still throwing an error: RangeError: Invalid time value.

import { format } from 'date-fns';
import { de } from 'date-fns/locale';

const date = new Date();

console.log( format(
  date, 'MMM', 
{ awareOfUnicodeTokens: true, locale: de}
) );
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

As you are using the FP version to format a date you need to use the formatWithOptions function to pass the locale options.

import format from "date-fns/fp/formatWithOptions";

It has a slightly different function signature then the format function, where you need to pass the options as the first argument.

const formatMonth = format({ locale: de }, "MMM");

Edit happy-hill-2e0mb

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to pass the Date as the first Parameter. For an example,

const formatMonth = format(new Date(),"MMM", {locale: de}); <br>

OR

const formatMonth = format(new Date(10/28/2021),"MMM", {locale: de});

enter image description here

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda