Project training record (4) - comparison of front-end date type and database timestamp type

Table of contents

1. What have you done recently?

2. Problems encountered and solutions

1. Axios gets the expiration time type in the database

2. The current time type of the Date() type obtained by the front end

3. The front and back end time types are uniformly converted into yyyy-MM-dd HH:mm:ss format

4. Time comparison in yyyy-MM-dd HH:mm:ss format

1. What have you done recently?

      Mainly record what was done from the end of April to the beginning of May, and some problems encountered.
      During this period of time, it is mainly the front-end writing and back-end logic writing of the client. I am mainly responsible for the display of application information on the client side and the display of application details.

      The front end involves comparing the application expiration time of timestamp type extracted from the database with the current time of Date type obtained by the front end, and then proceeds to the next logical judgment and operation.

       So let's talk about the method of time comparison.

2. Problems encountered and solutions

1. Axios gets the expiration time type in the database

The format of the obtained timestamp type:

即:yyyy-MM-dd'T'HH:mm:ss.SSSXXX

2. The current time type of the Date() type obtained by the front end

3. The front and back end time types are uniformly converted into yyyy-MM-dd HH:mm:ss format

In order to facilitate millisecond-level comparison, both are converted into yyyy-MM-dd HH:mm:ss format

The backend conversion method, that is, the method code from yyyy-MM-dd'T'HH:mm:ss.SSSXXX to yyyy-MM-dd HH:mm:ss is as follows:

renderTime(date){
      var dateee = new Date(date).toJSON();
      return new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
    },

The front-end conversion method, namely Date() to yyyy-MM-dd HH:mm:ss method code is as follows:

dateFormat(date){
      console.log("当前时间转换前:"+ date);
    
      let y = date.getFullYear()
      let m = date.getMonth() + 1
      m = m < 10 ? ('0' + m) : m
      let d = date.getDate()
      d = d < 10 ? ('0' + d) : d
      let h =date.getHours()
      h = h < 10 ? ('0' + h) : h
      let M =date.getMinutes()
      M = M < 10 ? ('0' + M) : M
      let s =date.getSeconds()
      s = s < 10 ? ('0' + s) : s
      let dateTime= y + '-' + m + '-' + d + ' ' + h + ':' + M + ':' + s;
      console.log("当前时间转换后:"+ dateTime);
      return dateTime;

    },

4. Time comparison in yyyy-MM-dd HH:mm:ss format

The Date.parse() method is used here.

The Date.parse() method parses a string containing a date and returns the number of milliseconds between that date and midnight January 1, 1970.

Strings are required by rules. As follows (drawing on some information and the blog of the big guy):

  • Short dates can use the /date separator, but must conform to the month/day/year format, such as 7/20/96 or 6/15/2008.
  • The year, month, and day in the long date expressed in the form of July 10 1995 can be arranged in any order, and the year can be expressed in 2-digit or 4-digit form. If a 2-digit year is used, the year must be greater than or equal to 70.
  • Any text within parentheses is considered a comment. These brackets can be nested.
  • Commas and spaces are considered separators. Multiple separators are allowed.
  • Month and day names must have two or more characters. If the two-character name is not unique, it resolves to the latest matching date. For example, Ju resolves to July, not June. Chrome does not support "Ju".
  • If a date is provided that states a week number that does not match the number of weeks determined from other parts of the date, the day of the week will be ignored. For example, Tuesday November 9 1996 is accepted and analyzed even though November 9, 1996 is actually a Friday. But the resulting  Date  object contains Friday November 9 1996.
  • JavaScript handles all standard time zones, as well as Coordinated Universal Time (UTC) and Greenwich Mean Time (GMT), for example: Thu, 07 Aug 2014 11:00:14 GMT. IE6 ~ IE8 have very little support for UTC format, and IE9's support is not comprehensive.
  • Hours, minutes, and seconds are separated by colons, but not all of these need to be specified. 10:, 10:11, and 10:11:12 are all valid.
  • It is an error to specify a time after 12 noon as PM when using a 24-hour clock. For example, 23:15 PM is wrong.
  • It is an error for strings containing invalid dates. For example, a string containing two years or two months is an error.

According to the rules, the format code for converting yyyy-MM-dd HH:mm:ss to MM-dd-yyyy HH:mm:ss is as follows:

compareDate(date1,date2){
        var startDate = date1.substring(0,10).split('-');//当前时间
				var endDate = date2.substring(0,10).split('-');//失效时间
				var startNum = startDate[1]+'-'+startDate[2]+'-'+startDate[0]+' '+date1.substring(10,19);  
				var endNum = endDate[1]+'-'+endDate[2]+'-'+endDate[0]+' '+date2.substring(10,19);
        
				var disparityTime =(Date.parse(endNum)-Date.parse(startNum))/3600/1000; //失效时间-当前时间
 				if(Number(disparityTime)<0){
				console.log("失效时间小于当前时间");
        return true;//已失效
 				}else{
           console.log("失效时间大于当前时间");
           return false;//未失效
         }

    },

Guess you like

Origin blog.csdn.net/pinkray_c/article/details/125108438