Date related: Get the date year, month and day (you can get the date of the previous or next few days), get the day of the week based on the date, date format conversion–convert the time string into a visual format

Use it as a public method and place it in util.js

In main.js

import util from '@/util/util'
Vue.prototype.$util = util

Get

1. Get the day of the week based on the date

Calling method:
let myDate = new Date(); //Get today’s date
let e = this.$util.getweekdayDate(myDate,0) // Pass 0 is today, 1 is tomorrow, -1 is yesterday, and so on
Code within the component:
getweekdayDate: function(date,day) {
  let days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] ;
  let today = new Date();
  today.setDate(today.getDate() + day);
  let weekday = today.getDay();
  let dd = days[weekday];
  return dd;
},

2. Year, month and day

Calling method:

let myDate = new Date(); //Get today’s date
this.$util.getNextDate(myDate,0) // Pass 0 is today, 1 is tomorrow, -1 is yesterday, and so on

Code within the component:

getNextDate: function(date,day) {
  const dd = new Date(date);
  dd.setDate(dd.getDate() + day);
  const y = dd.getFullYear();
  const m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
  const d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
  return y + "-" + m + "-" + d;
},

3. Year, month, day, hour, minute and second

Calling method:

let myDate = new Date(); //Get today’s date
this.$util.getNextDate(myDate,0) // Pass 0 is today, 1 is tomorrow, -1 is yesterday, and so on

Code within the component:

getNextDate(date,day) {
  var dd = new Date(date);
  dd.setDate(dd.getDate() + day);
  var y = dd.getFullYear();
  var m = dd.getMonth() + 1 < 10 ? "0" + (dd.getMonth() + 1) : dd.getMonth() + 1;
  var d = dd.getDate() < 10 ? "0" + dd.getDate() : dd.getDate();
  var h = dd.getHours() < 10 ? "0" + dd.getHours() : dd.getHours();
  var t = dd.getMinutes() < 10 ? "0" + dd.getMinutes() : dd.getMinutes();
  var s = dd.getSeconds() < 10 ? "0" + dd.getSeconds() : dd.getSeconds();
  let time = y + "-" + m + "-" + d + ' ' + h + ':' + t + ':' + s
  return time;
},

Four. Year, month, day, day of the week, hours, minutes and seconds

var date = new Date();
this.year = date.getFullYear();
this.month = date.getMonth() + 1;
this.date = date.getDate();
this.day = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday") [date.getDay()];
this.hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
this.minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
this.second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

Conversion

1. Date format conversion–converting time string into visual format

// Usage example: this.$util.formatDate('/Date(1435646188067 + 0800)/','yyyy-MM-dd hh:mm:ss')
// Usage example: this.$util.formatDate('2018-12-12T12:12:12','yyyy-MM-dd hh:mm:ss')
// Usage example: this.$util.formatDate('2018-12-12 12:12:12','yyyy/MM/dd hh:mm:ss')
// Usage example: this.$util.formatDate(new Date(),'yyyy/MM/dd hh:mm:ss')
// Called as a method: this.$util.formatDate(time,[format])
// Called as filter: {<!-- -->{time | formatDate([format])}}
formatDate: function(v, format) {
  if (!v) return ''
  var d = v
  if (typeof v === 'string') {
    if (v.indexOf('/Date(') > -1) {
      d = new Date(parseInt(v.replace('/Date(', '').replace(')/', ''), 10))
    } else {
      d = new Date(Date.parse(v.replace(/-/g, '/').replace('T', ' ').split('.')[0] ))
    } // .split(".")[0] is used to handle the situation where milliseconds appear and intercept .xxx, otherwise an error will occur
  }
  var o = {
    'M + ': d.getMonth() + 1,
    'd + ': d.getDate(),
    'h + ': d.getHours(),
    'm + ': d.getMinutes(),
    's + ': d.getSeconds(),
    'q + ': Math.floor((d.getMonth() + 3) / 3),
    'S': d.getMilliseconds()
  }
  if (/(y + )/.test(format)) {
    format = format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k] ).length))
    }
  }
  return format
},

2. Time format data is converted into just n minutes ago n hours ago today hh:mm yesterday hh:mm MM/dd hh:mm yyyy/MM/dd hh:mm

//The time format can be time object; yyyy/MM/dd hh:mm:ss;yyyy-MM-dd hh:mm:ss;
'/Date(1435646188067 + 0800)/';'2018-12-12T12:12:12'; and other legal time formats
formatTimestamp: function(v) {
  const timeStr = this.formatDate(v, 'yyyy/MM/dd hh:mm:ss')
  const timestamp = parseInt(new Date(timeStr).getTime() / 1000);
  function zeroize(num) {
    return (String(num).length === 1 ? '0' : '') + num;
  }
  var curTimestamp = parseInt(new Date().getTime() / 1000); // Current timestamp
  var timestampDiff = curTimestamp - timestamp; // The number of seconds difference between the parameter timestamp and the current timestamp
  var curDate = new Date(curTimestamp * 1000); // Current time and date object
  var tmDate = new Date(timestamp * 1000); // The date object converted from the parameter timestamp
  var Y = tmDate.getFullYear();
  var m = tmDate.getMonth() + 1;
  var d = tmDate.getDate();
  var H = tmDate.getHours();
  var i = tmDate.getMinutes();
  // var s = tmDate.getSeconds();
  if (timestampDiff < 60) { // Within one minute
    return 'just';
  } else if (timestampDiff < 3600) { // Within one hour ago
    return Math.floor(timestampDiff / 60) + 'minutes ago';
  } else if (curDate.getFullYear() === Y & amp; & amp; curDate.getMonth() + 1 === m & amp; & amp; curDate.getDate() === d) {
    return 'Today ' + zeroize(H) + ':' + zeroize(i);
  } else {
    var newDate = new Date((curTimestamp - 86400) * 1000); // The timestamp in the parameter plus one day is converted into a date object
    if (newDate.getFullYear() === Y & amp; & amp; newDate.getMonth() + 1 === m & amp; & amp; newDate.getDate() === d) {
      return 'yesterday ' + zeroize(H) + ':' + zeroize(i);
    } else if (curDate.getFullYear() === Y) {
      return zeroize(m) + '/' + zeroize(d) + ' ' + zeroize(H) + ':' + zeroize(i);
    } else {
      // return Y + '/' + zeroize(m) + '/' + zeroize(d) + ' ' + zeroize(H) + ':' + zeroize(i);
      return Y + '/' + zeroize(m) + '/' + zeroize(d);
    }
  }
},

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Vue entry skill tree Home page Overview 39702 people are learning the system