Formatting the date
When you view a post you may notice that the date is not formatted in an easy to read way.
Frontity in the browser
Let's fix that now. Doing this gives us the opportunity to use an external library with Frontity. Installing and using external JavaScript libraries is something that you're likely to need to do in real-world project development, so it's well worth covering here.
Frontity will happily work with almost any package available on npmjs.com. There are a number of packages for formatting dates, probably the best known of which is Moment. However, Moment is a large package so in the interests of streamlining our project as much as possible we're instead going to use the more lightweight Day.js to format our date string.
If, like us, you prefer to keep your bundle sizes as small as possible bundlephobia.com is a great resource for finding alternatives to large and heavyweight npm packages.
Open your terminal and ensure that you're in the root directory of your project. You can then install Day.js by running the following command:
1
npm install dayjs
Copied!
If you check the package.json file in the root of your project directory you will now see that dayjs has been added as a dependency.
Dependencies in package.json
Next, since the <Post> component is the only one that's going to use it, let's import Day.js into our post.js file:
1
// File: /packages/my-first-theme/src/components/post.js
2
import React from "react"
3
import { connect } from "frontity"
4
import dayjs from "dayjs"
5
// ...
Copied!
We now need to create a Day.js object and we pass in post.date, which is the tricky to read date string that is currently displaying in our post. We can do that with dayjs(post.date), and we then call the format function of the dayjs object passing it a formatting string.
We'll store the result in a variable (which we'll call formattedDate) for later use.
1
// File: /packages/my-first-theme/src/components/post.js
2
​
3
// ...
4
const formattedDate = dayjs(post.date).format("DD MMMM YYYY")
5
// ...
Copied!
See the Day.js documentation for other possible formatting strings. For example, try replacing DD MMMM YYYY above with MMM DD, YYYY or DD-MM-YY to see how the appearance of the date changes. Use whichever version of the formatted date that you prefer.
Finally we'll use the formattedDate variable holding our formatted date in our component by replacing post.date with it.
Your post.js file should now look like this:
1
// File: /packages/my-first-theme/src/components/post.js
2
import React from "react"
3
import { connect } from "frontity"
4
import dayjs from "dayjs"
5
​
6
const Post = ({ state }) => {
7
const data = state.source.get(state.router.link)
8
const post = state.source[data.type][data.id]
9
const author = state.source.author[post.author]
10
​
11
const formattedDate = dayjs(post.date).format("DD MMMM YYYY")
12
​
13
return (
14
<div>
15
<h2>{post.title.rendered}</h2>
16
<p>
17
<strong>Posted: </strong>
18
{formattedDate}
19
</p>
20
<p>
21
<strong>Author: </strong>
22
{author.name}
23
</p>
24
<div dangerouslySetInnerHTML={{ __html: post.content.rendered }} />
25
</div>
26
)
27
}
28
​
29
export default connect(Post)
Copied!
Our date is now formatted in a much more pleasing way.
Frontity in the browser
Check you're on the right track by comparing your changes with the ones in this commit.
Last modified 5mo ago
Copy link