Mastering Date and Time Formatting in EJS Templates
EJS (Embedded JavaScript Templates) is a powerful tool for generating dynamic HTML content. One common challenge developers face is displaying dates and times in a user-friendly format. This article explores how to customize date and time formatting within your EJS templates.
The Problem: Dates and Times in EJS
Imagine you have a blog application where you want to display the date each post was published. In your EJS template, you might have something like this:
<%- post.createdAt %>
While this will display the date and time, it likely won't be in a format your users will find intuitive. You might see something like "2023-10-26T14:22:15.000Z", which is not very user-friendly.
The Solution: EJS and Date Formatting
EJS doesn't have built-in date formatting functionality. Instead, it relies on JavaScript's native Date
object and its methods to manipulate dates and times. Here's how you can format dates in your EJS template:
<%- new Date(post.createdAt).toLocaleDateString() %>
This code snippet creates a new Date
object from the post.createdAt
timestamp and then uses the toLocaleDateString()
method to format it according to the user's locale. The result will be a date string formatted in a way that makes sense for the user's region.
Customization is Key
The toLocaleDateString()
method is a starting point, but you can further customize the date and time format using various options:
1. toLocaleDateString()
with Options:
<%- new Date(post.createdAt).toLocaleDateString('en-US', { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }) %>
This example uses the en-US
locale and specifies options to display the weekday, year, month, and day in a specific format.
2. Using Moment.js:
For more complex formatting, you can use a library like Moment.js. It provides a wide range of date and time manipulation capabilities:
<%- moment(post.createdAt).format('MMMM Do YYYY, h:mm:ss a') %>
This code snippet uses the moment()
function to parse the post.createdAt
timestamp and then formats it using the specified format string.
Additional Tips and Considerations
- Timezone Handling: Remember to be mindful of timezones when displaying dates and times. Ensure the timestamps you're working with are in the correct timezone.
- Locale Awareness: Make your application user-friendly by providing locale-specific date and time formats. Consider using
Intl.DateTimeFormat
or Moment.js for this purpose. - User Preferences: Allow users to customize the display of dates and times if possible. This provides a personalized experience.
Conclusion
Mastering date and time formatting in EJS templates is crucial for creating engaging and user-friendly applications. By leveraging JavaScript's Date
object, its formatting methods, and libraries like Moment.js, you can display dates and times in a way that meets your application's needs and enhances the user experience.
Remember: Always test your date and time formatting across different browsers and locales to ensure consistent results.