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

189
Views
Filtering some dates when they are of type string, relative to the user's locale

So I'm getting some dates from my backend.

I only need the date (without the time) and the format should be relative to the user's browser settings.

I've chosen this approach:

function dateFormatter(params) {
    if (!params.value || params.value == 'None') return ''
    return new Date(params.value).toLocaleString(undefined, options = { year: 'numeric', month: 'numeric', day: '2-digit' });
}

So far so good. It is working fine. Passing undefined as the first parameter makes it take whatever locale the user's using.

The problem is that now, for those strings returned by this function, I need a comparator. Something like this:

filterParams: {
            // provide comparator function
            comparator: (filterLocalDateAtMidnight, cellValue) => {
                const dateAsString = cellValue;

                if (dateAsString == null) {
                    return 0;
                }

                // In the example application, dates are stored as dd/mm/yyyy
                // We create a Date object for comparison against the filter date
                const dateParts = dateAsString.split('/');
                const day = Number(dateParts[2]);
                const month = Number(dateParts[1]) - 1;
                const year = Number(dateParts[0]);
                const cellDate = new Date(year, month, day);

                // Now that both parameters are Date objects, we can compare
                if (cellDate < filterLocalDateAtMidnight) {
                    return -1;
                } else if (cellDate > filterLocalDateAtMidnight) {
                    return 1;
                }
                return 0;
            }

How can I implement this comparator? Because my date format is dynamic according to the user, I can't just split dateParts[] like this example does.

Thanks.

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!