Skip to content

Commit

Permalink
refactor: refactor generate date function
Browse files Browse the repository at this point in the history
  • Loading branch information
izhichao committed Jun 16, 2024
1 parent cd49d2d commit 4b621f1
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion posts/post1.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 文章 1
datetime: '2023/10/01 10:00:00'
datetime: '2023-10-01 10:00:00'
permalink: /posts/cd49a3
outline: deep
pinned: true
Expand Down
2 changes: 1 addition & 1 deletion posts/post2.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 文章 2
datetime: '2023/10/02 10:00:00'
datetime: '2023-10-02 10:00:00'
permalink: /posts/1e9071
outline: deep
tags:
Expand Down
2 changes: 1 addition & 1 deletion posts/post3.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 文章 3
datetime: '2023/10/03 10:00:00'
datetime: '2023-10-03 10:00:00'
permalink: /posts/d4bad3
outline: deep
tags:
Expand Down
2 changes: 1 addition & 1 deletion posts/post4.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 文章 4
datetime: '2023/10/04 10:00:00'
datetime: '2023-10-04 10:00:00'
permalink: /posts/73a18e
outline: deep
tags:
Expand Down
2 changes: 1 addition & 1 deletion posts/post5.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 文章 5
datetime: '2023/10/05 10:00:00'
datetime: '2023-10-05 10:00:00'
permalink: /posts/e07056
outline: deep
pinned: true
Expand Down
2 changes: 1 addition & 1 deletion posts/sub-posts/post10.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 文章 10 - 摘要 demo
datetime: '2023/10/10 10:00:00'
datetime: '2023-10-10 10:00:00'
permalink: /posts/36fb63
outline: deep
tags:
Expand Down
2 changes: 1 addition & 1 deletion posts/sub-posts/post6.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 文章 6
datetime: '2023/10/06 10:00:00'
datetime: '2023-10-06 10:00:00'
permalink: /posts/c10a87
outline: deep
tags:
Expand Down
2 changes: 1 addition & 1 deletion posts/sub-posts/post7.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 文章 7
datetime: '2023/10/07 10:00:00'
datetime: '2023-10-07 10:00:00'
permalink: /posts/b808f8
outline: deep
tags:
Expand Down
2 changes: 1 addition & 1 deletion posts/sub-posts/post8.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 文章 8
datetime: '2023/10/08 10:00:00'
datetime: '2023-10-08 10:00:00'
permalink: /posts/2fc5ad
outline: deep
tags:
Expand Down
2 changes: 1 addition & 1 deletion posts/sub-posts/post9.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: 文章 9 - 图片 demo
datetime: '2023/10/10 10:00:00'
datetime: '2023-10-10 10:00:00'
permalink: /posts/0e1cd7
outline: deep
tags:
Expand Down
20 changes: 13 additions & 7 deletions src/utils/getPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export const getPosts = async ({
}

if (!data.datetime) {
data.datetime = await generateBirthtime(postPath);
const stats = await fs.stat(postPath);
data.datetime = formatDate(stats.birthtime);
flag = true;
}

Expand Down Expand Up @@ -182,12 +183,17 @@ const generateRandomString = (length: number) => {
return randomCode;
};

const generateBirthtime = async (path: string) => {
const stats = await fs.stat(path);
const formatDate = (date: string | Date) => {
if (typeof date === 'string') {
date = new Date(date);
}

let ISOString = stats.birthtime.toISOString().split('T');
const date = ISOString[0];
const time = ISOString[1].split('.')[0];
let year = date.getFullYear();
let month = String(date.getMonth() + 1).padStart(2, '0');
let day = String(date.getDate()).padStart(2, '0');
let hours = String(date.getHours()).padStart(2, '0');
let minutes = String(date.getMinutes()).padStart(2, '0');
let seconds = String(date.getSeconds()).padStart(2, '0');

return `${date} ${time}`;
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};

0 comments on commit 4b621f1

Please sign in to comment.