Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format created of transaction with date time value if the created date is the current date #34784

Merged
merged 12 commits into from
Jan 29, 2024
35 changes: 27 additions & 8 deletions src/libs/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,12 +360,19 @@ function getMicroseconds(): number {
return Date.now() * CONST.MICROSECONDS_PER_MS;
}

/**
* Returns the format yyyy-MM-dd HH:mm:ss of a date in the format expected by the database
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okey, let's nuke this comment (as it's a helper function related to getDBTime) and update the original comment so it makes sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't realize that you aren't the author of the phrase I commented earlier

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sound good, updated the comment.

*/
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/**
 * 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);
}

/**
Expand Down Expand Up @@ -733,13 +740,6 @@ function formatToSupportedTimezone(timezoneInput: Timezone): Timezone {
};
}

/**
* Returns the time in milliseconds of a date in the format expected by the database
*/
function getDBTimeFromDate(date: Date): string {
return date.toISOString().replace('T', ' ').replace('Z', '');
}

/**
* Return the date with full format if the created date is the current date.
* Otherwise return the created date.
Expand All @@ -749,6 +749,24 @@ function enrichMoneyRequestTimestamp(created: string): string {
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
*
* param {Date} inputDate
* returns {number}
*/
function getLastBusinessDayOfMonth(inputDate: Date): number {
let currentDate = endOfMonth(inputDate);
const dayOfWeek = getDay(currentDate);

if (dayOfWeek === 0) {
currentDate = subDays(currentDate, 2);
} else if (dayOfWeek === 6) {
currentDate = subDays(currentDate, 1);
}

return getDate(currentDate);
}

const DateUtils = {
formatToDayOfWeek,
Expand Down Expand Up @@ -795,6 +813,7 @@ const DateUtils = {
isTimeAtLeastOneMinuteInFuture,
formatToSupportedTimezone,
enrichMoneyRequestTimestamp,
getLastBusinessDayOfMonth,
};

export default DateUtils;
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.