1. Date formatting

      / * * *@description: Date formatting function 1 *@param Date Date type *@param Format The default date format is YYYY-MM-dd HH: MM :ss */
      function transform(date, format = 'yyyy-MM-dd HH:mm:ss') {
        const theCurrentDate = new Date(date.getTime()); // Convert the date format
        const year = theCurrentDate.getFullYear().toString();
        format = format.replace('yyyy', year); // Replace the year
        const month = (theCurrentDate.getMonth() + 1).toString().padStart(2.'0');
        format = format.replace('MM', month); // Replace month
        const day = theCurrentDate.getDate().toString().padStart(2.'0');
        format = format.replace('dd', day); / / replace day
        const hour = theCurrentDate.getHours().toString().padStart(2.'0');
        format = format.replace('HH', hour); // Replace hours
        const minutes = theCurrentDate.getMinutes().toString().padStart(2.'0');
        format = format.replace('mm', minutes); // Replace minutes
        const second = theCurrentDate.getSeconds().toString().padStart(2.'0');
        format = format.replace('ss', second); / / replace seconds
        return format;
      }
      
      
      / * * *@description: Date formatting function 2 *@param Date Date type *@param Format The default date format is YYYY-MM-dd HH: MM :ss */
      function formatDate(date, format = 'YYYY-MM-dd HH: MM :ss week W') {
        if(! date) {return date;
        }
        const typeDate = date instanceof Date ? date.getTime() : date;
        date = new Date(typeDate);
        const obj = {
          yyyy: date.getFullYear(), // Complete year example: 2021 -> 2021
          yy: (' ' + date.getFullYear()).slice(-2), // For example: 2021 -> 21
          M: date.getMonth() + 1.// If there are less than two digits in a month, zero is not added
          MM: ('0' + (date.getMonth() + 1)).slice(-2), // If there are less than two digits in a month, 0 is added
          d: date.getDate(), // If there are less than two digits in a day, zero is not added
          dd: ('0' + date.getDate()).slice(-2), // If the day is less than two digits, 0 is added
          H: date.getHours(), // Zero is not added if there are less than two digits in 24 hours
          HH: ('0' + date.getHours()).slice(-2), // Zero is added in less than two digits in 24 hours
          h: date.getHours() % 12.// In the 12-hour system, zero is not added if there are less than two digits
          hh: ('0' + (date.getHours() % 12)).slice(-2), // In the 12-hour system, 0 is added for less than two digits
          m: date.getMinutes(),  // Zero is not added if there are less than two bits per minute
          mm: ('0' + date.getMinutes()).slice(-2), // Add 0 to less than 2 bits per minute
          s: date.getSeconds(),  // Zero is not added if there are less than two bits per second
          ss: ('0' + date.getSeconds()).slice(-2),  // Second less than two bits fill 0
          w: ['day'.'一'.'二'.'三'.'four'.'five'.'六'][date.getDay()], / / week
        };
        return format.replace(/([a-z]+)/gi.function (key) {
          return obj[key];
        });
      }
      console.log(formatDate('the 2021-07-03 19:00:00'));
      // 2021-07-03 19:00:00 Saturday
Copy the code

2. Reverse time

     / * * *@description: Push forward and backward for hours *@param The date date *@param Num Specifies the number of hours to be reversed. If the value is negative, it is the number of hours to be reversed *@return Return date format */
      function beforeHours(date, num) {
        if(! date) {return date;
        }
        const typeDate = date instanceof Date ? date.getTime() : date;
        date = new Date(typeDate);
        const theCurrentDate = new Date(date.getTime());
        theCurrentDate.setHours(theCurrentDate.getHours() - (num || 0));
        return theCurrentDate;
      }
      / * * *@description: Pushes backwards by *@param The date date *@param Num Indicates the number of days to be reversed. If the value is negative, it indicates the number of days to be reversed *@return Return date format */
      function beforeDays(date, num) {
        if(! date) {return date;
        }
        const typeDate = date instanceof Date ? date.getTime() : date;
        date = new Date(typeDate);
        const theCurrentDate = new Date(date.getTime());
        theCurrentDate.setDate(theCurrentDate.getDate() - (num || 0));
        return theCurrentDate;
      }
      / * * *@description: Push the month backwards *@param The date date *@param If num is negative, it is the backward month *@return Return date format */
      function beforeMonths(date, num) {
        if(! date) {return date;
        }
        const typeDate = date instanceof Date ? date.getTime() : date;
        date = new Date(typeDate);
        const theCurrentDate = new Date(date.getTime());
        theCurrentDate.setMonth(theCurrentDate.getMonth() - (num || 0));
        return theCurrentDate;
      }
Copy the code

Testing:

      console.log(formatDate(beforeMonths('the 2021-07-03 12:00:00'.2)));
      console.log(formatDate(beforeDays('the 2021-07-03 12:00:00'.4)));
      console.log(formatDate(beforeHours('the 2021-07-03 12:00:00'.5)));
      console.log(formatDate(beforeHours('the 2021-07-03 12:00:00', -1)));
Copy the code

Like a little like oh ~