diff --git a/src/libs/DateUtils.ts b/src/libs/DateUtils.ts index 526769723531..dde9cf28148a 100644 --- a/src/libs/DateUtils.ts +++ b/src/libs/DateUtils.ts @@ -360,12 +360,18 @@ function getMicroseconds(): number { return Date.now() * CONST.MICROSECONDS_PER_MS; } +function getDBTimeFromDate(date: Date): string { + return date.toISOString().replace('T', ' ').replace('Z', ''); +} + /** - * Returns the current time in milliseconds in the format expected by the database + * Convert the given timestamp to the "yyyy-MM-dd HH:mm:ss" format, as expected by the database + * + * @param [timestamp] the given timestamp (if omitted, defaults to the current time) */ function getDBTime(timestamp: string | number = ''): string { const datetime = timestamp ? new Date(timestamp) : new Date(); - return datetime.toISOString().replace('T', ' ').replace('Z', ''); + return getDBTimeFromDate(datetime); } /** @@ -733,6 +739,15 @@ function formatToSupportedTimezone(timezoneInput: Timezone): Timezone { }; } +/** + * Return the date with full format if the created date is the current date. + * Otherwise return the created date. + */ +function enrichMoneyRequestTimestamp(created: string): string { + const now = new Date(); + const createdDate = parse(created, CONST.DATE.FNS_FORMAT_STRING, now); + return isSameDay(createdDate, now) ? getDBTimeFromDate(now) : created; +} /** * Returns the last business day of given date month * @@ -796,6 +811,7 @@ const DateUtils = { getWeekEndsOn, isTimeAtLeastOneMinuteInFuture, formatToSupportedTimezone, + enrichMoneyRequestTimestamp, getLastBusinessDayOfMonth, }; diff --git a/src/libs/actions/IOU.js b/src/libs/actions/IOU.js index eb9541edcad2..07ee7367cefd 100644 --- a/src/libs/actions/IOU.js +++ b/src/libs/actions/IOU.js @@ -870,6 +870,7 @@ function createDistanceRequest(report, participant, comment, created, category, // If the report is an iou or expense report, we should get the linked chat report to be passed to the getMoneyRequestInformation function const isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report); const currentChatReport = isMoneyRequestReport ? ReportUtils.getReport(report.chatReportID) : report; + const currentCreated = DateUtils.enrichMoneyRequestTimestamp(created); const optimisticReceipt = { source: ReceiptGeneric, @@ -881,7 +882,7 @@ function createDistanceRequest(report, participant, comment, created, category, comment, amount, currency, - created, + currentCreated, merchant, userAccountID, currentUserEmail, @@ -906,7 +907,7 @@ function createDistanceRequest(report, participant, comment, created, category, createdIOUReportActionID, reportPreviewReportActionID: reportPreviewAction.reportActionID, waypoints: JSON.stringify(validWaypoints), - created, + created: currentCreated, category, tag, billable, @@ -1290,6 +1291,7 @@ function requestMoney( // If the report is iou or expense report, we should get the linked chat report to be passed to the getMoneyRequestInformation function const isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report); const currentChatReport = isMoneyRequestReport ? ReportUtils.getReport(report.chatReportID) : report; + const currentCreated = DateUtils.enrichMoneyRequestTimestamp(created); const {payerAccountID, payerEmail, iouReport, chatReport, transaction, iouAction, createdChatReportActionID, createdIOUReportActionID, reportPreviewAction, onyxData} = getMoneyRequestInformation( currentChatReport, @@ -1297,7 +1299,7 @@ function requestMoney( comment, amount, currency, - created, + currentCreated, merchant, payeeAccountID, payeeEmail, @@ -1320,7 +1322,7 @@ function requestMoney( amount, currency, comment, - created, + created: currentCreated, merchant, iouReportID: iouReport.reportID, chatReportID: chatReport.reportID,