Become a Skillthrive author!

Convert time

By Hunter Becton on May 12, 2022

const convertTime = time => {
  return new Date(time).toLocaleDateString('en-us', {
    year: 'numeric',
    month: 'long',
    day: 'numeric',
  })
}

How to use

Read our lesson on how to format dates with Vanilla JavaScript if you want a detailed explanation of how this code snippet works, including how to customize it further for your desired format.

The snippet above takes a timestamp, say one from a MongoDB database, and converts it to a format like June 12, 2022. For example, an ISO 8601 timestamp like '2022-03-24T17:12:40+00:00' would be formatted to read as March 24, 2022.