当前位置:网站首页>Wechat applet timestamp conversion time format + time subtraction

Wechat applet timestamp conversion time format + time subtraction

2022-06-11 16:33:00 EOPG

One 、 The timestamp is converted to days , when , branch , second

  1. newly build ***util.js, Copy the following code
/**  Timestamp format days  **/
function computationTime (total) {
    
	total = total / 1000;
   // Calculate integer days 
   let day = parseInt(total / (24 * 60 * 60));
   // Gets the number of seconds remaining after the number of days is calculated 
   let afterDay = total - day * 24 * 60 * 60;
   // Calculate integer hours 
   let hour = parseInt(afterDay / (60 * 60));
   // Gets the number of seconds remaining after the calculated hours 
   let afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60;
   // Calculate integral minutes 
   let min = parseInt(afterHour / 60);
   // Calculate the number of seconds left after minutes 
   let afterMin = total - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60;
   return {
    
    day,
    hour,
    min,
    afterMin: Math.ceil(afterMin)
   };
  }
  module.exports = {
    
    computationTime: computationTime
  }
  1. In need of js Introduced inside
import {
    computationTime} from "../../utils/jpxutil.js";
  1. Direct reference to tool functions , Enter timestamp return days , when , branch , second
    let time = new Date("2022-06-10 11:40:01");
    let time1 = new Date("2022-06-09 10:40:00");
    let tt = time.getTime() - time1.getTime();
    console.log(computationTime(tt));

Two 、 Subtracting time into days , when , branch , second

Basically consistent with the above

Code up


/**  Time minus  */
function timeDifference (newTime, oldTime){
    
    let time1 = new Date(newTime);
    let time2 = new Date(oldTime);
    return computationTime(time1 - time2);
}
/**  Timestamp format days  **/
function computationTime (total) {
    
    total = total / 1000;
   // Calculate integer days 
   let day = parseInt(total / (24 * 60 * 60));
   // Gets the number of seconds remaining after the number of days is calculated 
   let afterDay = total - day * 24 * 60 * 60;
   // Calculate integer hours 
   let hour = parseInt(afterDay / (60 * 60));
   // Gets the number of seconds remaining after the calculated hours 
   let afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60;
   // Calculate integral minutes 
   let min = parseInt(afterHour / 60);
   // Calculate the number of seconds left after minutes 
   let afterMin = total - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60;
   return {
    
    day,
    hour,
    min,
    afterMin: Math.ceil(afterMin)
   };
  }
  module.exports = {
    
    computationTime: computationTime,
    timeDifference: timeDifference
  }
原网站

版权声明
本文为[EOPG]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111617149721.html