24 lines
625 B
JavaScript
24 lines
625 B
JavaScript
import { useI18n } from 'vue-i18n';
|
|
|
|
export default function (value, options = {}) {
|
|
if (!value) return;
|
|
|
|
if (!isValidDate(value)) return null;
|
|
|
|
if (!options.dateStyle && !options.timeStyle) {
|
|
options.day = '2-digit';
|
|
options.month = '2-digit';
|
|
options.year = 'numeric';
|
|
}
|
|
|
|
const { locale } = useI18n();
|
|
const newDate = new Date(value);
|
|
|
|
return new Intl.DateTimeFormat(locale.value, options).format(newDate);
|
|
}
|
|
// handle 0000-00-00
|
|
function isValidDate(date) {
|
|
const parsedDate = new Date(date);
|
|
return parsedDate instanceof Date && !isNaN(parsedDate.getTime());
|
|
}
|