Project training record - Comparison between front-end date type and database timestamp type

Tip: after the article is written, the directory can be automatically generated. Please refer to the help text on the right for how to generate it

1, What have you done recently?

It mainly records what has been done from the end of April to the beginning of May, as well as some problems encountered.
During this period, it is mainly the front-end writing and back-end logic writing of the client. I am mainly responsible for displaying the client application information and application details.

The front end involves comparing the application expiration time of the timest amp type extracted from the database with the current time of the Date type obtained by the front end, and then making 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

Obtained timestamp type format:

Namely: yyyy MM DD t HH: mm: ss SSSXXX

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

 

3. the front and back time types are uniformly converted to yyyy MM DD hh:mm:ss format

For the convenience of millisecond comparison, both are converted to yyyy MM DD hh:mm:ss format

Back end conversion method, namely yyyy MM DD t HH: mm: ss Sssxxx to yyyy MM DD hh:mm:ss method codes are 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, i.e. Date() to yyyy MM DD hh:mm:ss method code is as follows:

dateFormat(date){
      console.log("Before current time conversion:"+ 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("After current time conversion:"+ dateTime);
      return dateTime;

    },

4. Time comparison of yyyy MM DD hh:mm:ss format

Date Parse() method.

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

Strings are required by rules. As follows (drawing on some materials and the boss' blog):

  • Short dates can use the / date separator, but must conform to the format of month / day / year, such as 7/20/96 or 6/15/2008.
  • The year, month and day in the long date in the form of July 10 1995 can be arranged in any order, and the year can be expressed in the form of 2 or 4 digits. If the year is represented by 2 digits, the year must be greater than or equal to 70.
  • Any text in parentheses is considered a comment. These parentheses can be nested.
  • Commas and spaces are treated as delimiters. Multiple delimiters are allowed.
  • Month and day names must have two or more characters. If a two character name is not unique, the name resolves to the latest matching date. For example, Ju resolves to July, not June. Chrome does not support "Ju".
  • If a Date is provided and the number of weeks stated in the Date does not match the number of weeks determined according to other parts of the Date period, the number of weeks in the Date will be ignored. For example, although November 9, 1996 is actually Friday, Tuesday November 9, 1996 can be accepted and analyzed. However, the Date object of the result contains Friday November 9 1996.
  • JavaScript handles all standard time zones and coordinates universal time (UTC) and Greenwich mean time (GMT), for example: Thu, 07 Aug 2014 11:00:14 GMT. IE6 ~ IE8 support UTC format is very low, and IE9 support is not comprehensive.
  • Separate hours, minutes, and seconds with colons, but not all of them need to be specified. 10: , 10:11 and 10:11:12 are valid.
  • If the clock used is 24 hours, it is wrong to designate the time after 12:00 noon as PM. For example, 23:15 PM is wrong.
  • A string containing an invalid date is incorrect. For example, a string containing two years or two months is incorrect.

According to the rules, convert yyyy MM DD HH:mm:ss to MM DD yyyy HH:mm:ss. The format code is as follows:

compareDate(date1,date2){
        var startDate = date1.substring(0,10).split('-');//current time 
				var endDate = date2.substring(0,10).split('-');//Expiration time
				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; //Expiration time - current time
 				if(Number(disparityTime)<0){
				console.log("The expiration time is less than the current time");
        return true;//Expired
 				}else{
           console.log("The expiration time is greater than the current time");
           return false;//Not expired
         }

    },

Tags: Vue Front-end programming language

Posted by genericnumber1 on Thu, 02 Jun 2022 22:27:30 +0530