JSFiddle - React, Tailwind, and code Playground
by bilbobaggins
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
JavaScript
const DATE= {
DATE_TIME: 'YYYY-MM-DD HH:mm:ss',
DATE_SHORT:'YYYY/MM/DD',
DATE_SHORT_ALT:'DD/MM/YYYY',
DATE_LONG:'DD MMMM YYYY',
DATE_LONG_ALT:'MMMM Do YYYY'
}
const format = ({ value, minFractionDigits, maxFractionDigits, format }) => {
if (value === undefined || value === 0) return '-'
const obj = { value }
if (minFractionDigits !== undefined) obj.minFractionDigits = minFractionDigits
if (maxFractionDigits !== undefined) obj.maxFractionDigits = maxFractionDigits
switch (format) {
case 'date':
//return formatDate(obj)
return formatDate(obj)
}
}
const dateFormat = ({ value = new Date(), sourceFormat, targetFormat = DATE.DATE_SHORT }) => {
return moment(value, sourceFormat).format(targetFormat)
}
console.log(format())