1. Preliminary understanding of ISO8601

  • ToISOString () gives you a standard ISO time in milliseconds (229 in the middle of.229z is a millisecond).
  • Date.parse() is then followed by a string of numbers (note: this string has no concept of a city).
  • Pass the resulting string of numbers to new Date() to get the new Date object
  • Any time can be expressed clearly in ISO8601

2. Use: day.js (not used because moment.js is too bulky, day.js has the same API but is much smaller)

  • Day.js.org/ (中文 原 文 : dayjs.giee.io/zh-cn /)

Here’s how to use an example

  • The first step

yarn add dayjs

  • The second step
import dayjs from 'dayjs';
Copy the code

  • The third step

Note: a day is 86400 seconds, because js units are milliseconds, need to multiply 1000.

const oneDay = 86400 * 1000;
Copy the code

  • Step 4: Function realization, display “the day before yesterday”, “yesterday”, “today”, display specific year month day, the current year display month day, the specific date format can be queried in the document format
const oneDay = 86400 * 1000; . @Component({}) ... export default class Xxxxxx extends Vue{ ... beautify(string: string) { const day = dayjs(string); const now = dayjs(); If (day. IsSame (now, 'day')) {return 'today '; } else if (day.issame (now.subtract(1,'day'),'day')) {return 'yesterday '; } else if(day.issame (now.subtract(2,'day'),'day')){return 'day before day'; }else if(day. IsSame (now,'year')){return day. Format (' yyyy-mm-dd ')}else{return day.Copy the code