Search code examples
javascriptdatedayjs

How to format date with Day.js in Vue.js


I am building a web project with mainly Vue.js and am having problems with date time formatting. When getting data from my database (which is in datetime format), I get something like '2021-01-08T00:00:00'. Now is there a way to make Day.js accept such an input and convert that to either 'dd-mm-yyyy' or 'mm-dd-yyyy' or whatever format I want? I have tried:

this.data.DateOfBirth = dayjs(e.dateOfBirth).format("dd-mm-yyyy");

But it doesn't seem to work and throws an error warning like this on the browser console:

The specified value "Su-01-yyyy" does not conform to the required format, "yyyy-MM-dd".

Many thanks!


Solution

  • format of the date is not correct.

    • dd -> day
    • mm -> minutes
    • DD -> Date
    • MM -> Month
    • YYYY -> Year

    There are other formats as well. Please check the required format on the docs and use as per the requirement.

    https://day.js.org/docs/en/display/format