当前位置:网站首页>微信小程序时间戳转化时间格式+时间相减
微信小程序时间戳转化时间格式+时间相减
2022-06-11 16:17:00 【EOPG】
微信小程序时间戳转化时间格式+时间相减
一、时间戳转化为天,时,分,秒
- 新建***util.js,复制下面代码
/** 时间戳格式化天数 **/
function computationTime (total) {
total = total / 1000;
//计算整数天数
let day = parseInt(total / (24 * 60 * 60));
//取得算出天数后剩余的秒数
let afterDay = total - day * 24 * 60 * 60;
//计算整数小时数
let hour = parseInt(afterDay / (60 * 60));
//取得算出小时数后剩余的秒数
let afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60;
//计算整数分
let min = parseInt(afterHour / 60);
//算出分后剩余的秒数
let afterMin = total - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60;
return {
day,
hour,
min,
afterMin: Math.ceil(afterMin)
};
}
module.exports = {
computationTime: computationTime
}
- 在需要的js里面引入
import {
computationTime} from "../../utils/jpxutil.js";
- 直接引用工具函数,输入时间戳返回天,时,分,秒
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));
二、时间相减转化为天,时,分,秒
与上面的基本一致
上代码
/** 时间相减 */
function timeDifference (newTime, oldTime){
let time1 = new Date(newTime);
let time2 = new Date(oldTime);
return computationTime(time1 - time2);
}
/** 时间戳格式化天数 **/
function computationTime (total) {
total = total / 1000;
//计算整数天数
let day = parseInt(total / (24 * 60 * 60));
//取得算出天数后剩余的秒数
let afterDay = total - day * 24 * 60 * 60;
//计算整数小时数
let hour = parseInt(afterDay / (60 * 60));
//取得算出小时数后剩余的秒数
let afterHour = total - day * 24 * 60 * 60 - hour * 60 * 60;
//计算整数分
let min = parseInt(afterHour / 60);
//算出分后剩余的秒数
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
}
边栏推荐
猜你喜欢

Detailed explanation of the functions of list and dict data types

Princeton Dengjia student's personal account: must I have a doctorate? No, I can also be an applied scientist in a large factory as an undergraduate
![Enterprise purchase, sales and inventory management system based on SSM framework [source code + database + design]](/img/af/b5b5a69654a28d252cc5954b5e847a.png)
Enterprise purchase, sales and inventory management system based on SSM framework [source code + database + design]

Laravel listening mode

时序预测 | MATLAB实现RBF径向基神经网络时间序列未来多步预测

2022年高处安装、维护、拆除考试模拟100题及在线模拟考试

Data enhancement

Factory calibrated gravity: working principle and installation position of carbon monoxide sensor, calibration standard description

项目工作区创建步骤-泽众AR自动化测试工具

Memory image of various data types in C language
随机推荐
Pytest测试框架基础篇
[learn FPGA programming from scratch -17]: quick start chapter - operation steps 2-5- VerilogHDL hardware description language symbol system and program framework (both software programmers and hardwa
jdbc调试错误,求指导
Time processing logic for the last 7 days, the last 10 days, and the last 90 days
Ruiji takeout project (III) employee management business development
leetcode417. 太平洋大西洋水流问题(中等)
2022起重机司机(限桥式起重机)考试题模拟考试题库及模拟考试
[leetcode daily question] Repeat overlay string matching
TC8:UDP_ MessageFormat_ 01-02
干掉 Swagger UI,这款神器更好用、更高效!
This "invisible" robot may be your future colleague
one hundred and twenty-three thousand one hundred and twenty-three
Laravel 8 realizes database backup through task scheduling
2022安全员-C证特种作业证考试题库及答案
Implementation of VGA protocol based on FPGA
项目工作区创建步骤-泽众AR自动化测试工具
电脑下面的任务栏怎么显示打开的程序
Analysis of time complexity and space complexity
Pytest test framework Basics
Student website template brown cake dessert website design - brown cake dessert shop (4 pages) gourmet dessert website production final assignment finished product_ Fresh fruit web design final assign