Locales - why does dayjs behave like this?

72 views Asked by At

I need all dates to be assumed as locale en-gb (ie DD/MM/YYYY)

import dayjs from 'dayjs'
import 'dayjs/locale/en-gb'
dayjs.locale('en-gb')

const date = '01/02/2000' // 1st Feb
console.log(dayjs(date).format('DD/MM/YYYY')) // 02/01/2000 (wrong!)

Why is it assuming that the date is in usa format, when I have already set the locale?

2

There are 2 answers

0
Tachibana Shin On

dayjs works correctly this is the international standard if you write xx-xx-xxxx or xx/xx/xxxx then the format is m/d/y. that means January 2, 2000

0
pallemry On

Its because javascript reads the date in 'MM/DD/YYYY' format so what you have to do is change it to:

console.log(dayjs(date, 'DD/MM/YYYY').format('DD/MM/YYYY')) // 01/02/2000 (correct!)