JavaScript の一般的な日付計算関数

JavaScript の一般的な日付計算関数

1. 2 つの期間の間の間隔を計算し、切り上げます。

    //计算开始日期和结束日期中间相差了多少天(接收参数为 date对象)
    computedDays(start, end) {
    
    
      if (start && end) {
    
    
        const day =
          (new Date(end).getTime() - new Date(start).getTime()) /
          3600 /
          1000 /
          24;
        return parseInt(day);
      }
    },

2. 終了時間が開始時間より大きいかどうかを判断するために使用されます。

    //判断结束时间是否大于开始时间(接收参数为 date对象)
    computedSrartIsBigEnd(start, end) {
    
    
      if (start && end) {
    
    
        if (new Date(end).getTime() - new Date(start).getTime() > 0) {
    
    
          return true;
        } else {
    
    
          return false;
        }
      }
    },

3. 現在の日付に基づいて n 日前を推定します。

	//根据当前日期向后推导n天(date为时间对象,n为向后推导天数)
	//返回值可以和4.格式化 使用
	nextDateByNum(date,n){
    
    
  		const nowTime = new Date(date);
        nowTime.setTime(nowTime.getTime() + 3600 * 1000 * 24 * n);
        return nowTime
	}
      

4. 時刻と日付の書式設定に使用されます(2 桁未満の月と日は自動的に 0 で埋められます)。

    //将时间戳 计算日期格式化 用于显示(接收参数为 date对象)
    handleData(date) {
    
    
      const timeDate = new Date(date);
      const year = timeDate.getFullYear();
      const month = (timeDate.getMonth() + 1).toString().padStart(2, 0);
      const day = timeDate.getDate().toString().padStart(2, 0);
      const hour = timeDate.getHours().toString().padStart(2, 0);
      const min = timeDate.getMinutes().toString().padStart(2, 0);
      const s = timeDate.getSeconds().toString().padStart(2, 0);
      return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + s;
    },

おすすめ

転載: blog.csdn.net/qq_51075057/article/details/127196873