Created
December 19, 2022 09:49
-
-
Save jkohlin/39928d028a8bec7d8d9b5d24f49f0356 to your computer and use it in GitHub Desktop.
Different options for date formatting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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