Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libs/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
subMilliseconds,
subMinutes,
} from 'date-fns';
import {formatInTimeZone, fromZonedTime, toZonedTime, format as tzFormat} from 'date-fns-tz';
import {formatInTimeZone, fromZonedTime, toDate, toZonedTime, format as tzFormat} from 'date-fns-tz';
import {enGB} from 'date-fns/locale/en-GB';
import {es} from 'date-fns/locale/es';
import throttle from 'lodash/throttle';
Expand Down Expand Up @@ -756,7 +756,7 @@ const getTimeValidationErrorKey = (inputTime: Date): string => {
* returns If the date is valid, returns the formatted date with the UTC timezone, otherwise returns an empty string.
*/
function formatWithUTCTimeZone(datetime: string, dateFormat: string = CONST.DATE.FNS_FORMAT_STRING) {
const date = new Date(datetime);
const date = toDate(datetime, {timeZone: 'UTC'});

if (isValid(date)) {
return tzFormat(toZonedTime(date, 'UTC'), dateFormat);
Expand Down
8 changes: 6 additions & 2 deletions src/libs/TransactionUtils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {parse} from 'date-fns';
import {format, isValid, parse} from 'date-fns';
import lodashDeepClone from 'lodash/cloneDeep';
import lodashHas from 'lodash/has';
import lodashIsEqual from 'lodash/isEqual';
Expand Down Expand Up @@ -558,7 +558,11 @@ function getPostedDate(transaction: OnyxInputOrEntry<Transaction>): string {
function getFormattedPostedDate(transaction: OnyxInputOrEntry<Transaction>, dateFormat: string = CONST.DATE.FNS_FORMAT_STRING): string {
const postedDate = getPostedDate(transaction);
const parsedDate = parse(postedDate, 'yyyyMMdd', new Date());
return DateUtils.formatWithUTCTimeZone(parsedDate.toDateString(), dateFormat);

if (isValid(parsedDate)) {
return DateUtils.formatWithUTCTimeZone(format(parsedDate, 'yyyy-MM-dd'), dateFormat);
}
return '';
}

/**
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/DateUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,12 @@ describe('DateUtils', () => {
expect(formattedDate).toEqual(expectedResult);
});
});

it('returns the correct date when the date with time is used', () => {
const datetimeStr = '2022-11-07 17:48:00';
const expectedResult = '2022-11-07';
expect(DateUtils.formatWithUTCTimeZone(datetimeStr)).toEqual(expectedResult);
});
});

describe('getLastBusinessDayOfMonth', () => {
Expand Down