Summary of javascript date tools

Summary of javascript date tools
let Utils = {
  /**
   * Is it the year of death? * @return {Boolse} true|false
   */
  isLeapYear: function(y) {
    return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0;
  },
  /**
   * Returns the week number * @return {Number}
   */
  getWhatDay: function(year, month, day) {
    let date = new Date(year + '/' + month + '/' + day);
    let index = date.getDay();
    let dayNames = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    return dayNames[index];
  },
  /**
   * Returns the week number * @return {Number}
   */
  getMonthPreDay: function(year, month) {
    let date = new Date(year + '/' + month + '/01');
    let day = date.getDay();
    if (day == 0) {
      day = 7;
    }
    return day;
  },
  /**
   * Returns the day of the month * @return {Number}
   */
  getMonthDays: function(year, month) {
    if (/^0/.test(month)) {
      month = month.split('')[1];
    }
    return [0, 31, this.isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30, 31][month];
  },
  /**
   * Fill in the digits * @return {string}
   */
  getNumTwoBit: function(n) {
    n = Number(n);
    return (n > 9 ? '' : '0') + n;
  },
  /**
   * Convert the date object to a string * @return {string}
   */
  date2Str: function(date, split) {
    if (typeof date == 'string') return date;
    split = split || '-';
    let y = date.getFullYear();
    let m = this.getNumTwoBit(date.getMonth() + 1);
    let d = this.getNumTwoBit(date.getDate());
    return [y, m, d].join(split);
  },
  /**
   * Returns a date format string * @param {Number} 0 returns today's date, 1 returns tomorrow's date, 2 returns the day after tomorrow's date, and so on * @return {string} '2014-12-31'
   */
  getDay: function(i) {
    i = i || 0;
    let date = new Date();
    let diff = i * (1000 * 60 * 60 * 24);
    date = new Date(date.getTime() + diff);
    return this.date2Str(date);
  },
  /**
   * Timestamp converted to date format * @return {String}
   */
  timestampToDate: function(timestamp) {
    let date = new Date(timestamp);
    return date.getFullYear() + '-' + getNumTwoBit(date.getMonth() + 1) + '-' + getNumTwoBit(date.getDate());
  },
  /**
   * Time comparison * @return {Boolean}
   */
  compareDate: function(date1, date2) {
    let startTime = new Date(date1.replace('-', '/').replace('-', '/'));
    let endTime = new Date(date2.replace('-', '/').replace('-', '/'));
    if (startTime >= endTime) {
      return false;
    }
    return true;
  },
  /**
   * Time comparison * @return {Boolean}
   */
  compareDateArr: function(date1, date2) {
    let startTime = new Date();
    startTime.setFullYear(parseInt(date1[0]), parseInt(date1[1]) - 1, parseInt(date1[2]));
    startTime.setHours(parseInt(date1[3]), parseInt(date1[4]));
    let endTime = new Date();
    endTime.setFullYear(parseInt(date2[0]), parseInt(date2[1]) - 1, parseInt(date2[2]));
    endTime.setHours(parseInt(date2[3]), parseInt(date2[4]));
    if (startTime >= endTime) {
      return false;
    }
    return true;
  },
  /**
   * Are the times equal? ​​* @return {Boolean}
   */
  isEqual: function(date1, date2) {
    let startTime = new Date(date1).getTime();
    let endTime = new Date(date2).getTime();
    if (startTime == endTime) {
      return true;
    }
    return false;
  },
  getDateArr(str) {
    return [this.getYear(str), this.getMonth(str), this.getDate(str), this.getHour(str), this.getMinute(str)];
  },
  isDateString(str) {
    return /\d{4}(\-|\/|.)\d{1,2}\1\d{1,2}/.test(str) || /^([01][0-9]|2[0-3])(:[0-5][0-9]){1,2}$/.test(str);
  },
  getYear(value) {
    return this.isDateString(value) ? value.split(' ')[0].split(/-|\/|\./)[0] : value.getFullYear();
  },
  getMonth(value) {
    return this.isDateString(value) ? value.split(' ')[0].split(/-|\/|\./)[1] : value.getMonth() + 1;
  },
  getDate(value) {
    return this.isDateString(value) ? value.split(' ')[0].split(/-|\/|\./)[2] : value.getDate();
  },
  getHour(value) {
    if (this.isDateString(value)) {
      const str = value.split(' ')[1] || '00:00:00';
      return str.split(':')[0];
    }
    return value.getHours();
  },
  getMinute(value) {
    if (this.isDateString(value)) {
      const str = value.split(' ')[1] || '00:00:00';
      return str.split(':')[1];
    }
    return value.getMinutes();
  }
};
export default Utils;

Summarize

This article ends here. I hope it can be helpful to you. I also hope that you can pay more attention to more content on 123WORDPRESS.COM!

You may also be interested in:
  • JavaScript DateUtils definition and usage examples
  • Detailed explanation of javascript date tool class DateUtils
  • Summary of 50+ Utility Functions in JavaScript
  • Java development tools - scala json format processing tool - json4s detailed explanation
  • Summary of JavaScript Common Tool Function Libraries

<<:  Markup Languages ​​- What to learn after learning HTML?

>>:  Mysql optimization tool (recommended)

Recommend

Dynamically edit data in Layui table row

Table of contents Preface Style Function Descript...

Quickly install MySQL5.7 compressed package on Windows

This article shares with you how to install the M...

MySQL SQL statement to find duplicate data based on one or more fields

SQL finds all duplicate records in a table 1. The...

HTML implements the function of detecting input completion

Use "onInput(event)" to detect whether ...

How to use sed command to efficiently delete specific lines of a file

Preface Normally, if we want to delete certain li...

CentOS7 uses rpm to install MySQL 5.7 tutorial diagram

1. Download 4 rpm packages mysql-community-client...

Vue3.0 routing automatic import method example

1. Prerequisites We use the require.context metho...

How to pass parameters to JS via CSS

1. Background that needs to be passed through CSS...

Comparative Analysis of MySQL Binlog Log Processing Tools

Table of contents Canal Maxwell Databus Alibaba C...

Summary of things to pay attention to in the footer of a web page

Lots of links You’ve no doubt seen a lot of sites ...

How to set the width attribute to the style of the span tag

If you directly set the width attribute to the sty...

How to configure SSL certificate in nginx to implement https service

In the previous article, after using openssl to g...

How to solve the problem that Docker container has no vim command

Find the problem Today, when I tried to modify th...

Tutorial on installing MySQL under Linux

Table of contents 1. Delete the old version 2. Ch...