Format conversion function:

export function formatDate (value, fmt) {
  let getDate = new Date(value);
  let o = {
    'M+': getDate.getMonth() + 1.'d+': getDate.getDate(),
    'h+': getDate.getHours(),
    'm+': getDate.getMinutes(),
    's+': getDate.getSeconds(),
    'q+': Math.floor((getDate.getMonth() + 3) / 3),
    'S': getDate.getMilliseconds()
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (getDate.getFullYear() + ' ').substr(4 - RegExp.$1.length))
  }
  for (let k in o) {
    if (new RegExp('(' + k + ') ').test(fmt)) {
      fmt = fmt.replace(RegExp. $1,RegExp.$1.length === 1)? (o[k]) : (('00' + o[k]).substr((' ' + o[k]).length)))
    }
  }
  return fmt;
}
Copy the code

Usage 1: Time stamp to time

let date1 = new Date().getTime();
let date1After = formatDate(date1, 'yyyy-MM-dd hh:mm:ss');
Copy the code

Usage two: Time format conversion

let date2 = '2021-3-25';
let date2After = formatDate(date2, 'yyyy/MM/dd hh:mm:ss');

let date3 = '2021-03-25';
let date3After = formatDate(date3, 'yyyy/MM/dd hh:mm:ss');

let date4 = '2021/03/25';
let date4After = formatDate(date4, 'yyyy/MM/dd hh:mm:ss');

let date5 = '2021/3/25';
let date5After = formatDate(date5, 'yyyy/MM/dd hh:mm:ss');

let date6 = '2021/03/25';
let date6After = formatDate(date6, 'yyyy/MM/dd hh:mm:ss');

let date7 = '2021-11-25';
let date7After = formatDate(date7, 'yyyy/MM/dd hh:mm:ss');
Copy the code

Reasons for the difference of 8 hours:

In ES5, if a date is separated by a conjunction line (-) with a leading 0 or two digits, JavaScript will assume that it is a date string in ISO(Greenwich Mean time zone) format, resulting in the time returned in UTC. Dates in other formats are considered non-ISO and the local time zone is used as the timing standard.

In ES6, the local time zone of the user is used for any date string that does not specify a time zone.