Skip to content

Date format incorrect on months with less than 31 days #442

@wimbarelds

Description

@wimbarelds

Describe the bug
A clear and concise description of what the bug is.

To Reproduce
Steps to reproduce the behavior:

  1. Be in a month with less than 31 days
  2. import { format } from '@pega/react-sdk-components/lib/components/helpers/formatters';
  3. Call format('2025-03-31')
  4. See result 01-03-2025 (dutch locale) or 03-01-2025 (us locale)

Expected behavior
Expected result: 31-03-2025 (dutch locale) or 03-31-2025 (us locale)

Additional context
This is caused by this code:

https://github.com/pegasystems/react-sdk-components/blob/master/packages/react-sdk-components/src/components/helpers/formatters/index.ts#L20-L24

const date = new Date();

date.setDate(day);
date.setMonth(month);
date.setFullYear(year);

First a date object is instantiated on the current date. Then it sets the day of the month to the 31st; if the current month does not have 31 days, then the date will overflow to the first of the next month (or beyond if the current month is februari), then the month and year are set.

How to fix this:

const date = new Date();
date.setFullYear(year, month, day);

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions