'how to properly use dayjs inside vue 3 app and component

I am able to use dayjs inside vue3 component by adding it to data()

import dayjs from 'dayjs'

export default {
  data() {
    return {
      dayjs
    }
  }
}

Then I will be able to use it inside template but is this the correct way to do?

What if I want to configure dayjs and use it globally? I tried

import dayjs from 'dayjs'
import { createApp } from 'vue'
const app = createApp(App)
    
app.use(dayjs) // doesn't work
app.dayjs = dayjs // doesn't work
    
app.mount("#app')

but couldn't get it to work so far.
What is the correct way to do it?



Solution 1:[1]

u can use

import dayjs from 'dayjs'
import { createApp } from 'vue'
const app  = createApp(App)
    
app.config.globalProperties.$dayjs = dayjs
    
app.mount("#app')

Solution 2:[2]

The accepted method does not seem to take into account composition API. My understanding is that the only way to use this with Composition API is to provide/inject. Example below working with composition API, options API in script and templates.

//[main.js]

import dayjs from 'dayjs' //import dayjs in your main.js
 
app.provide('dayJS', dayjs) // provide dayJS

app.use(router).mount("#app") // mount app

// [component.js]

// Composition API setup ------------------
import { inject } from 'vue' // import inject from vue
const dayJS = inject("dayJS") // inject dayJS

//make sure that you return dayJS in setup along with any functions/variables
return { dayJS }

// Options API setup ------------------
app.component('mycomponent', {
  inject: ['dayJS'],
  created() {
    console.log(dayJS())  
  }
})

//You can now use dayJS directly within setup ex: dayJS() or template ex: {{dayJS()}}.

Solution 3:[3]

You can use provide/inject to use dayjs inside of your component.

//main.js
import dayjs from 'dayjs'
import { createApp } from 'vue'

const app = createApp({
  provide() {
    return {
      $dayjs: dayjs // <-- provide 
    }
  },    
    
app.mount("#app')


//myComponent.vue
<template>
    DajsJS: {{ myDays }}
</template>

<script>
export default {
  name: 'myComponent',
  inject: ['$dayjs'], // <-- inject
  computed: {
    myDays() {
      return this.$dayjs('1990-01-01')
    }
  }
}
</script>

Solution 4:[4]

If you are using composition api you can use direct dayjs without having to pass it through a provider. Look at the following example.

<template>
  <section>
    <h1>Título de ejemplo</h1>
    <h2>
      Fecha de creación 
      {{ dayjs('Fri Dec 17 2021 00:55:42 GMT-0500 (hora estándar de Colombia)').format('DD/MM/YYYY HH:mm') }}
    </h2>
    <h3>
      Otra prueba {{ date }}
    </h3>
  </section>
</template>

<script lang="ts">
import { defineComponent, computed, ref } from "vue";

import dayjs from "dayjs";

export default defineComponent({
  name: 'Ejemplo',
  setup() {

    const date_example = ref<string>('Fri Dec 17 2021 00:55:42 GMT-0500 (hora estándar de Colombia)');

    const date = computed<string>((): string => {
      return dayjs(date_example.value).format('DD/MM/YYYY HH:mm');
    });

    return {
      dayjs,
      date,
    }
  }
});
</script>

Solution 5:[5]

//[main.js]

import dayjs from "dayjs"; //import dayjs in your main.js
app.provide("dayJS", dayjs); // provide dayJS
app.config.globalProperties.$dayjs = dayjs; // //You can now use dayjs as $dayjs

app.use(router).mount("#app") // mount app

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ???? ??????
Solution 2
Solution 3 Hexodus
Solution 4 Andersson Mesa
Solution 5 Asadbek Eshboev