current position:Home>Use day in Vue JS (time and date processing library)

Use day in Vue JS (time and date processing library)

2022-05-15 02:13:33Maple moving forward

install :npm install dayjs --save

introduce :

var dayjs = require('dayjs') //commonjs
//import dayjs from 'dayjs' // ES 2015

If you need to use a plug-in , Then you need to introduce plug-ins and use :

eg:

import relativeTime from "dayjs/plugin/relativeTime"; // import relativeTime plugin

dayjs.extend(relativeTime); //  use

dayjs The default is English , If necessary, you can load other languages :

eg: import "dayjs/locale/zh-cn"; // chinese

Supported languages :https://github.com/iamkun/dayjs/tree/dev/src/locale

Use :

<template>
   <div>{
   {aaa}}</div> // Use it directly 
   <div v-for="item in xxx">
       {
   { dayjs(item).locale("zh-cn").fromNow() }} // Cannot be used directly 
       {
   { _dayjs(item).locale("zh-cn").fromNow() }} // Can be dayjs Set as the current instance property 
   </div>
</template>

import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import "dayjs/locale/zh-cn";
dayjs.extend(relativeTime);

export default {
  data() {
    return {
        _dayjs:''
    };
  },
  created() {
     this.aaa = dayjs("2020-8-01").locale("zh-cn").fromNow();
     this._dayjs = dayjs;
  }
};



Mount to global :

main.js
import dayjs from "dayjs";
import relativeTime from "dayjs/plugin/relativeTime";
import "dayjs/locale/zh-cn";
dayjs.extend(relativeTime);
Vue.prototype.$dayJS = dayjs;
console.log(Vue.prototype.$dayJS);
 Gets or sets the date of the month :
dayjs().date()
dayjs().date(1)

 Gets or sets the day of the week :
dayjs().day()
dayjs().day(0)

 Get or set ISO week :
dayjs.extend(isoWeek)
dayjs().isoWeekday()
dayjs().isoWeekday(1); // Monday

 Plus time : Returns a cloned Day.js object , And add the specified time .
dayjs().add(7, 'day')  //day week month ...
 Similarly, subtract time :
dayjs().subtract(7, 'year') //...

 Before the specified time :
dayjs.extend(relativeTime)
var a = dayjs()
var b = dayjs('1990-01-01')
a.from(b) // 2 years ago

...

 

copyright notice
author[Maple moving forward],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/132/202205120524480230.html

Random recommended