Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@RickStrahl
Last active April 11, 2019 23:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickStrahl/12a239ff89a2acf32baa4a2bfe250ff4 to your computer and use it in GitHub Desktop.
Save RickStrahl/12a239ff89a2acf32baa4a2bfe250ff4 to your computer and use it in GitHub Desktop.

You can do simple date formatting with a library by using the Intl.DateTimeFormat JavaScript class.

MDN for DateTimeFormat

alert( formatDate(new Date()));
// Apr 11, 2019, 1:10:40 PM

function formatDate(date, locale) {
	if (!locale)
  	locale = 'en-us';

    return new Intl.DateTimeFormat(locale, {
      year: 'numeric',
      day: 'numeric',
      month: 'short',
      hour: 'numeric',
      minute: 'numeric',      
      hour12: true,
    }).format(date);
    
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment