Skip to content

Instantly share code, notes, and snippets.

@tomfordweb
Created December 4, 2018 00:18
Show Gist options
  • Save tomfordweb/8614246bce3a02c307a56692437a7b5f to your computer and use it in GitHub Desktop.
Save tomfordweb/8614246bce3a02c307a56692437a7b5f to your computer and use it in GitHub Desktop.
ES6 Format timestamp to locale string
/**
* Returns a nice date, dude!
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toLocaleDateString
* @param {string} timestamp A timestamp waiting to be parsed
* @pram {object} options See the link provided above
* @param {String} locale locale string
* @return {string} A formatted timestamp
*/
export const niceDate = (timestamp, options = {}, locale = "en-US") => {
const mergedOptions = {
...{ weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' },
...options,
};
return new Date(timestamp).toLocaleDateString(locale, mergedOptions);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment