-
Notifications
You must be signed in to change notification settings - Fork 883
fix: simplify date formatting in getDate function #581
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
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
It's directly create this PR by VSCode that I haven't edit the PR info... This uses a more convenient method to create the timestamp for 這使用了一個更加便捷的方法來創建 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR simplifies the date formatting logic in the getDate
function by replacing manual date component extraction and formatting with the built-in toISOString()
method.
- Removes manual year, month, and day extraction and padding logic
- Replaces custom date formatting with
toISOString()
method call
const day = String(today.getDate()).padStart(2, "0") | ||
|
||
return `${year}-${month}-${day}` | ||
return new Date().toISOString() |
Copilot
AI
Aug 6, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The toISOString() method returns a full ISO 8601 timestamp (e.g., '2023-08-15T14:30:00.000Z') instead of just the date portion (e.g., '2023-08-15'). This breaks the expected format and likely affects downstream code that expects only the date. Consider using toISOString().split('T')[0] to extract just the date portion.
return new Date().toISOString() | |
return new Date().toISOString().split('T')[0] |
Copilot uses AI. Check for mistakes.
In // // Retrieve posts and sort them by publication date
async function getRawSortedPosts() {
const allBlogPosts = await getCollection("posts", ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true;
});
const sorted = allBlogPosts.sort((a, b) => {
const dateA = new Date(a.data.published);
const dateB = new Date(b.data.published);
return dateA > dateB ? -1 : 1;
});
return sorted;
} The method uses the built-in In the project, I tried to search for the
export function formatDateToYYYYMMDD(date: Date): string {
return date.toISOString().substring(0, 10);
}
function formatDate(date: Date) {
const month = (date.getMonth() + 1).toString().padStart(2, "0");
const day = date.getDate().toString().padStart(2, "0");
return `${month}-${day}`;
} They all use the |
The date initialized in new-post.js serves not only as a sorting criterion but also functions as the article's Frontmatter, which will be used as the actual display date and time. |
Yes, it will be recorded in the It's hard to balance readability and the difficulty of editing. For me, I might edit several articles in a day, but the previous method couldn't sort my articles by editing time well. |
I think this is a reasonable change. It makes sense for posts created on the same day to be sorted. The times on the built pages should have already been re-formatted, so this change shouldn't affect the display. I think the only problem here is that the ISO 8601 format is less readable, but it shouldn't be a big problem since users usually don't need to edit the generated time. To make it less unreadable, I suggest we remove the parts after the seconds and use a format like |
I got it... I will put the possible modifications below. function getDate() {
const today = new Date()
const year = today.getFullYear()
const month = String(today.getMonth() + 1).padStart(2, "0")
const day = String(today.getDate()).padStart(2, "0")
const hours = String(today.getHours()).padStart(2, "0")
const minutes = String(today.getMinutes()).padStart(2, "0")
const seconds = String(today.getSeconds()).padStart(2, "0")
return `${year}-${month}-${day}T${hours}:${minutes}:${seconds}`
} What needs to be considered is that there is a possible time zone problem here. If we ignore the time zone problem and keep the format just mentioned above, then a simple approach may be like this. function getDate() {
return date.toISOString().substring(0, 19);
} |
I think this is ok. We don't need to worry about the time zone since the time within the day is only used for sorting. |
好吶———— |
No description provided.