Convert time

A function to convert hard-to-read timestamps and date strings into a readable format using only vanilla JavaScript and the built-in toLocaleString method.

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.