Skip to content

Instantly share code, notes, and snippets.

@balsimpson
Last active October 19, 2022 08:10
Show Gist options
  • Select an option

  • Save balsimpson/fe1b8975c22cf3e7825e245acd428f40 to your computer and use it in GitHub Desktop.

Select an option

Save balsimpson/fe1b8975c22cf3e7825e245acd428f40 to your computer and use it in GitHub Desktop.
A simple function to convert a date to a human readable string
// format date with time and AM/PM
/**
* A simple function to convert a date to a human readable string
* @param date a date object or a string that can be parsed into a date object
* @returns string formatted as "D MMM YYYY, hh:mm am/pm"
*/
const getFormattedDate = (date) => {
return date.toLocaleString('en-GB', { day: 'numeric', month: 'short', year: 'numeric', hour: 'numeric', minute: 'numeric', hour12: true })
}
let d = getFormattedDate(new Date());
let nd = getFormattedDate("11 Oct 2022, 7:52 pm");
console.log(nd); // 11 Oct 2022, 7:52 pm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment