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

221
Views
Get string format for date based on locale

I am trying to get the format string for dates like "dd/MM/yyyy" based on locale. I want to use it in a picker date component to change the default format based on language.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I have a solution but I would like others if is possible.

function getDateFormatPattern(locale: string) {
    const getPatternForPart = (part: Intl.DateTimeFormatPart) => {
        switch (part.type) {
            case 'day':
                return 'd'.repeat(part.value.length);
            case 'month':
                return 'M'.repeat(part.value.length);
            case 'year':
                return 'y'.repeat(part.value.length);
            case 'literal':
                return part.value;
        }
    };

    return new Intl.DateTimeFormat(locale).formatToParts(new Date('2022-01-01'))
        .map(getPatternForPart)
        .join('');
};
about 4 years ago · Juan Pablo Isaza 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!