Search code examples
javascriptdayjs

From Object to dayJs


Is it possible to create a dayjs with and Object?.

For example, in moment we have.

   let newDate = moment({years:2000,months:1,days:1})

which will create a moment date.


Solution

  • The answer is yes, it is possible since the version 1.8.26, for doing this you need a plugin wich you install on your needs.

    Here's a little example on how to do it, in case you want to implement it on the browser

    dayjs.extend(window.dayjs_plugin_objectSupport)
    let x = dayjs({
      year: 2010,
      month: 1,
      day: 12
    })
    
    console.log(x)
    <script src="https://unpkg.com/dayjs@1.8.36/plugin/objectSupport.js"></script>
    <script src="https://unpkg.com/dayjs@1.8.36/dayjs.min.js"></script>