javascript - javascript - 获取一周中第二天的日期?
问题描述
例如,如何获取下周一的日期和下午 5:30 的时间,并计算当前日期和时间与该日期和时间之间的差异?
如果我现在在 2020 年 8 月 28 日 17:35 运行它,它应该给我 2020 年 8 月 31 日 17:30 和 2 天 23 小时 55 分钟的差异。
解决方案
这是工作示例:
function nextWeekMonday(date)
{
var diff = date.getDate() - date.getDay() + (date.getDay() === 0 ? -6 : 1);
var currWeekMonday = new Date(date.setDate(diff));
return new Date(currWeekMonday.getTime() + 7 * 24 * 60 * 60 * 1000);
}
function getDateDifference(current, future) {
// get total seconds between the times
var delta = Math.abs(future - current) / 1000;
// calculate (and subtract) whole days
var days = Math.floor(delta / 86400);
delta -= days * 86400;
// calculate (and subtract) whole hours
var hours = Math.floor(delta / 3600) % 24;
delta -= hours * 3600;
// calculate (and subtract) whole minutes
var minutes = Math.floor(delta / 60) % 60;
delta -= minutes * 60;
// what's left is seconds
var seconds = delta % 60;
return `${days} Days, ${hours} Hours, ${minutes} Minutes, ${seconds} Seconds`;
}
var curr = new Date; // get current date
var nextMonday = nextWeekMonday(curr);
console.log(getDateDifference(curr, nextMonday));
推荐阅读
- aws-lambda - 在 aws lambda 上安装 azure-cli 包
- html - 如何在相同的属性上分别触发过渡,以及如何过渡渐变?
- batch-file - 使用 Windows 批处理脚本自动解压缩任务
- jira - 如何为单个项目的自定义字段的每个问题类型指定默认值?
- cookies - Cookie “PHPSESSID” 将很快被视为针对
因为方案不匹配 - minio - 镜像存储桶时的权限问题,我正在尝试将一个 minio 服务器的存储桶迁移到另一个
- java - Mysql "Select mod(col1, 10) ..." 生成 bigint。如何使其成为int类型
- javascript - 如何设置图像的高度和宽度,以使其适合我在 fabric.js 中的画布大小?
- c - Telnet 拒绝简单 tcp 服务器上的连接
- python - 使用 Python 模拟通过随机创建的方阵的液体流动,该方阵包含一组 0-9 的整数