Show chat message date and time in YYYY/MM/DD format#189
Conversation
Replace time-only formatting with formatMessageDateTime so each bubble shows the calendar date (year/month/day) next to the localized time. Made-with: Cursor
There was a problem hiding this comment.
Code Review
This pull request updates the chat message timestamp display by renaming the utility function formatMessageTime to formatMessageDateTime and modifying it to include the date alongside the time. I have kept the feedback regarding the potential breaking change of renaming the utility and the suggestion to add input validation for the date string to prevent invalid output.
|
|
||
| export function formatMessageTime(dateString: string): string { | ||
| return new Date(dateString).toLocaleTimeString(undefined, { | ||
| export function formatMessageDateTime(dateString: string): string { |
There was a problem hiding this comment.
Renaming formatMessageTime to formatMessageDateTime is a breaking change. If this utility is used in other parts of the application not included in this PR, those components will fail to import the function. Please verify that all occurrences have been updated or provide a backward-compatible alias.
| export function formatMessageDateTime(dateString: string): string { | ||
| const date = new Date(dateString); | ||
| const y = date.getFullYear(); | ||
| const m = String(date.getMonth() + 1).padStart(2, '0'); | ||
| const d = String(date.getDate()).padStart(2, '0'); | ||
| const time = date.toLocaleTimeString(undefined, { | ||
| hour: '2-digit', | ||
| minute: '2-digit', | ||
| }); | ||
|
|
||
| return `${y}/${m}/${d} ${time}`; | ||
| } |
There was a problem hiding this comment.
The formatMessageDateTime function lacks validation for the input dateString. If an invalid or empty string is provided, new Date(dateString) will result in an Invalid Date, causing the function to return a string with NaN values (e.g., "NaN/NaN/NaN Invalid Date"). Adding a check for date validity ensures the UI remains clean even with unexpected data.
export function formatMessageDateTime(dateString: string): string {
const date = new Date(dateString);
if (isNaN(date.getTime())) {
return '';
}
const y = date.getFullYear();
const m = String(date.getMonth() + 1).padStart(2, '0');
const d = String(date.getDate()).padStart(2, '0');
const time = date.toLocaleTimeString(undefined, {
hour: '2-digit',
minute: '2-digit',
});
return `${y}/${m}/${d} ${time}`;
}
Summary
formatMessageDateTimeinresources/js/composables/useChat.tsbuilds the date;MessageBubble.vueuses it next to the sender name.Verification
npm run build(optional; run locally if you need a production asset refresh).Made with Cursor