Merge pull request #97 from Music47ell/v1

refactor: use formatDate util
This commit is contained in:
Timothy 2021-06-29 23:12:52 +08:00 committed by GitHub
commit 58ec53d19d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 14 deletions

View File

@ -3,8 +3,7 @@ import Tag from '@/components/Tag'
import siteMetadata from '@/data/siteMetadata'
import { useState } from 'react'
import Pagination from '@/components/Pagination'
const postDateTemplate = { year: 'numeric', month: 'long', day: 'numeric' }
import formatDate from '@/lib/utils/formatDate'
export default function ListLayout({ posts, title, initialDisplayPosts = [], pagination }) {
const [searchValue, setSearchValue] = useState('')
@ -58,9 +57,7 @@ export default function ListLayout({ posts, title, initialDisplayPosts = [], pag
<dl>
<dt className="sr-only">Published on</dt>
<dd className="text-base font-medium leading-6 text-gray-500 dark:text-gray-400">
<time dateTime={date}>
{new Date(date).toLocaleDateString(siteMetadata.locale, postDateTemplate)}
</time>
<time dateTime={date}>{formatDate(date)}</time>
</dd>
</dl>
<div className="space-y-3 xl:col-span-3">

View File

@ -3,8 +3,7 @@ import PageTitle from '@/components/PageTitle'
import SectionContainer from '@/components/SectionContainer'
import { BlogSeo } from '@/components/SEO'
import siteMetadata from '@/data/siteMetadata'
const postDateTemplate = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }
import formatDate from '@/lib/utils/formatDate'
export default function PostLayout({ frontMatter, authorDetails, next, prev, children }) {
const { date, title } = frontMatter
@ -20,9 +19,7 @@ export default function PostLayout({ frontMatter, authorDetails, next, prev, chi
<div>
<dt className="sr-only">Published on</dt>
<dd className="text-base font-medium leading-6 text-gray-500 dark:text-gray-400">
<time dateTime={date}>
{new Date(date).toLocaleDateString(siteMetadata.locale, postDateTemplate)}
</time>
<time dateTime={date}>{formatDate(date)}</time>
</dd>
</div>
</dl>

14
lib/utils/formatDate.js Normal file
View File

@ -0,0 +1,14 @@
import siteMetadata from '@/data/siteMetadata'
const formatDate = (date) => {
const options = {
year: 'numeric',
month: 'long',
day: 'numeric',
}
const now = new Date(date).toLocaleDateString(siteMetadata.locale, options)
return now
}
export default formatDate

View File

@ -3,9 +3,9 @@ import { PageSeo } from '@/components/SEO'
import Tag from '@/components/Tag'
import siteMetadata from '@/data/siteMetadata'
import { getAllFilesFrontMatter } from '@/lib/mdx'
import formatDate from '@/lib/utils/formatDate'
const MAX_DISPLAY = 5
const postDateTemplate = { year: 'numeric', month: 'long', day: 'numeric' }
export async function getStaticProps() {
const posts = await getAllFilesFrontMatter('blog')
@ -37,9 +37,7 @@ export default function Home({ posts }) {
<dl>
<dt className="sr-only">Published on</dt>
<dd className="text-base font-medium leading-6 text-gray-500 dark:text-gray-400">
<time dateTime={date}>
{new Date(date).toLocaleDateString(siteMetadata.locale, postDateTemplate)}
</time>
<time dateTime={date}>{formatDate(date)}</time>
</dd>
</dl>
<div className="space-y-5 xl:col-span-3">