[Number of days between two dates and times]

Current date and target date interval

The comparison between the current date and the target date is sometimes used in front-end development.
The following is the commonly used js method.
It should be noted that the iOS system sometimes cannot recognize the date in the horizontal slash format, and needs to be converted into a slash date. The specific writing method is also given below.

the interval between two dates

// 当前日期与目标日期相隔几天
export const getDiffDay = (date_1, date_2) => {
       var totalDays = 0;
	   var diffDate = 0;
	   // 第一种写法  iOS系统无法识别带有横杠的日期(2022-09-09 12:09:11),在进行时间戳转化的时候会直接返回Nan。 
       // var myDate_1 = Date.parse(date_1);
       // var myDate_2 = Date.parse(date_2);
       
       // 第二种写法 将带有横杠的日期转化成斜杠的日期。这样再转成时间戳的时候 iOS系统就可以识别。
	   var myDate_1 = new Date(date_1.replace(/-/g, '/')).getTime();
	   var myDate_2 = new Date(date_2.replace(/-/g, '/')).getTime();

        // 两个时间戳相减值
       diffDate = Math.abs(myDate_1 - myDate_2) ;
       // 判断当前日期与目标日期一致 返回0
	   if((myDate_1 - myDate_2) === 0){
		   totalDays = 0;
	   }
	   // 判断当前日期大于目标日期 返回-1
	   else if((myDate_1 - myDate_2) > 0){
		    totalDays = -1;
	   }
	   // 判断当前日期小于目标日期 返回具体天数
	   else{
		   totalDays = Math.floor(diffDate / (1000 * 3600 * 24));
	   }
	   console.log("间隔天数",totalDays);
	   
       return totalDays 
 }

get current date

// 获取当前日期
export function getCurrentTime() {
    var date = new Date();
    var year = date.getFullYear(); //月份从0~11,所以加一
    let month = date.getMonth();
    // console.log("month",month);
    var dateArr = [
        date.getMonth() + 1,
        date.getDate(),
        date.getHours(),
        date.getMinutes(),
        date.getSeconds(),
    ];
    //如果格式是MM则需要此步骤,如果是M格式则此循环注释掉
    for (var i = 0; i < dateArr.length; i++) {
        if (dateArr[i] >= 1 && dateArr[i] <= 9) {
            dateArr[i] = "0" + dateArr[i];
        }
    }
	
    var strDate =
        year +
        "-" +
        dateArr[0] +
        "-" +
        dateArr[1] +
        " " +
        dateArr[2] +
        ":" +
        dateArr[3] +
        ":" +
        dateArr[4];
        //此处可以拿外部的变量接收  strDate:2022-05-01 13:25:30
       // console.log("strDate",strDate);
	return strDate;
}

The above two js methods can be written in public files.

Instructions

Introduce the js header file in the file that needs to be used. For example below

// 引入头文件
import {getCurrentTime, getDiffDay} from '@/common/utils/Util'

// 我这边是直接在渲染里面使用,所以需要初始化一下。如果是方法里面使用就不需要这一步了。
mounted() {
			this.getCurrentTime = getCurrentTime;
			this.getDiffDay = getDiffDay;

		}

// 页面渲染使用
<view class="coupon-item-balck" v-if="getDiffDay(getCurrentTime(),目标时间值) == 0"></view>
		

Precautions

In the example, there is a display control in which the current time is equal to the target time.
And both dates are in the format '2022-02-01 12:09:08'.
So unified processing is done in the above js method.
If the date format in the actual project is already a slash, then there is no need to process it in js.
The above method is for reference only, and the actual project still needs to be processed according to the demand.

Guess you like

Origin blog.csdn.net/c1o2c3o4/article/details/130502559