Vue 3 Date time Format in moment.js ? vue file
<template>
<div>
<p class="text-danger">
{{ dateTime(category.created_at) }}
</p>
</div>
</template>
<script>
import moment from "moment";
export default {
computed: {
dateTime(value) {
return moment(value).format("YYYY-MM-DD");
},
},
};
</script>
getting error
http://prntscr.com/10z3p80
The error says "Cannot convert object to primitive value"
Looks like moment is expecting a date value in string format, but you are passing in an object. Try passing in a string and hopefully that works.
Thanks for your reply. I got another error
http://prntscr.com/10z4lv5
I don't know the moment.js support Vue 3 or not. Please give me suggestions which will be best for the date and time conversion in Vue 3
Thanks
Computer properties in Vue can't take in parameters. Change it to a method instead.
Moment is fine, but I usually use date-fns these days: https://date-fns.org/
If I just need to do something simple, I may not even use a library at all.
I am very glad to get your suggestions. I request a little bit you said you may not use the library so can I get some code suggestions that how I could convert " 2021-03-29T13:56:16.000000Z " this type of data and time?
Thanks
Probably still easier to use a library in this case.
If you weren't you could make use of the Date Object in vanilla JS
const dateObject = new Date('2021-03-29T13:56:16.000000Z')
dateObject.getFullYear() // 2021
dateObject.getMonth() // 2
dateObject.getDate() // 29
Again, not the easiest for a few reasons.
getMonth() is 0 based so you have to add 1 to it
You'd have to add a leading zero to getMonth and getDate
It has been solved by using the method
methods: {
dateTime(value) {
return moment(value).format("YYYY-MM-DD");
}
Thanks @drehimself
Please sign in or create an account to participate in this conversation.