Skip to content

Instantly share code, notes, and snippets.

@b4n92uid
Last active July 12, 2022 19:54
Show Gist options
  • Save b4n92uid/c38e5a5bc6cbca134bf56cf1a23917bc to your computer and use it in GitHub Desktop.
Save b4n92uid/c38e5a5bc6cbca134bf56cf1a23917bc to your computer and use it in GitHub Desktop.
[Vue] DateMixin
import { format, parseISO, formatDistanceToNow } from "date-fns";
import { fr, arDZ } from "date-fns/locale";
const locales = { fr, ar: arDZ };
function formatDate(str) {
if (typeof str === "string") str = parseISO(str);
return format(str, "dd/MM/yyyy");
}
function formatDateTime(str) {
if (typeof str === "string") str = parseISO(str);
return format(str, "dd/MM/yyyy HH:mm:ss");
}
function formatDateTimeNow(str, locale) {
if (typeof str === "string") str = parseISO(str);
return formatDistanceToNow(str, { addSuffix: true, locale });
}
export default {
methods: {
formatDate,
formatDateTime,
formatDateTimeNow(str) {
return formatDateTimeNow(str, locales[this.$i18n.locale]);
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment