Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

User1980's avatar

Best way to format date without moment js in vue2?

Hi all,

Let’s say you have a date like this in a loop:

<ul>
<span v-for="item in data">
<li>
<small>{{item.created_at }}</small>
</li>
</span>
</ul>

item.created_at outputs: 2021-11-22 16:32:52

I would like to format it to: 22-11-2021

How can I do this in a loop without using moment.js please?

Thank you,

0 likes
10 replies
User1980's avatar
User1980
OP
Best Answer
Level 4

@Sinnbeck Yes the size + it starts to give some errors about ISO format and their support is not responding much anymore. Other people on Reddit said that it was better to leave moment.js as it was not well maintained anymore. Thanks for this tiny Library, I am going to check it out now.

martinbean's avatar

@user1980 22-11-2021 isn’t really a recognised date format.

You’re better off using JavaScript’s native Date object to format the date as a localised format. That way, the user will see the date in a format they’ll recognise and be familiar with rather than your arbitrary reverse MySQL format.

martinbean's avatar

@Sinnbeck Even then, just doing new Date(originalString).toLocaleDateString() will yield a date string formatted to the user’s locale.

2 likes
User1980's avatar

@martinbean any idea how to pass this in vue when used in a a loop as I cannot get it to show anything when used like this: data.created_at comes from Laravel as DD-MM-YYYY(via model casting)

<div v-for="x in xx">
{{new Date(x.created_at).toLocaleDateString()}}
</div>

Thanks

Please or to participate in this conversation.