Skip to content

Instantly share code, notes, and snippets.

@jkohlin
Created December 19, 2022 09:49
Show Gist options
  • Save jkohlin/39928d028a8bec7d8d9b5d24f49f0356 to your computer and use it in GitHub Desktop.
Save jkohlin/39928d028a8bec7d8d9b5d24f49f0356 to your computer and use it in GitHub Desktop.
Different options for date formatting
/**
* Description
* @param {Date} {date
* @param {number} offset=0
* @param {'nice|short|iso|none':string} format='nice'}
* @returns {Date}
*/
export const niceDate = ({ date, offset = 0, format = 'nice' }) => {
const offsetDate = getOffsetDate(date, offset)
switch (format) {
case 'nice':
return offsetDate.toLocaleDateString('sv-SE', { year: 'numeric', month: 'long', day: 'numeric' })
case 'short':
return `${offsetDate.getDate()} ${offsetDate.toLocaleString('sv-SE', { month: 'short' })}`
case 'iso':
return offsetDate.toLocaleDateString('sv-SE')
case 'none':
default:
return offsetDate
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment