gpt4 book ai didi

javascript - 如何从特定时间(例如上午 9 点到下午)以分钟为单位开始计时

转载 作者:行者123 更新时间:2023-11-30 20:01:52 26 4
gpt4 key购买 nike

我想从上午 9 点到晚上 9 点以分钟为单位开始计时

最佳答案

回答标题中的问题:

您可以创建一个Date 并使用setHourssetMinutes 等将其时间设置为早上9 点。

然后您可以使用 - 找出两个日期之间的毫秒差值。除以 1000 * 60,您可以计算出以分钟为单位的差异。

const minutesSinceNineInTheMorning = (dateTime) => {
const nineInTheMorning = new Date(dateTime);
nineInTheMorning.setHours(9);
nineInTheMorning.setMinutes(0);
nineInTheMorning.setSeconds(0);
nineInTheMorning.setMilliseconds(0);

const msInAMinute = 1000 * 60;

return (dateTime - nineInTheMorning) / msInAMinute;
};

console.log(minutesSinceNineInTheMorning(new Date()));

剩下的问题是找到 block 之间的重叠,既然您有常规数字可以使用,这应该是可行的。

如果您仍然需要解决最后一个问题的帮助,最好提出一个新问题或展示一些特定于所需逻辑的尝试。

附言这是我在午休时间写的一个简单示例,可能会帮助您入门:

const requestByJohn = { start: 220, end: 280, name: "John" };
const options = [
{start: 225, end: 285, name: "Jane" },
{start: 210, end: 270, name: "Aisha"},
{start: 180, end: 240, name: "Brad" },
{start: 180, end: 330, name: "Alice"},
{start: 300, end: 360, name: "Yusef"},
{start: 270, end: 330, name: "Rob" }
];

const match = req => opt => ({
// Calculate the overlap between two blocks
overlap: opt.end <= req.start || opt.start > req.end
? 0
: Math.min(req.end, opt.end) - Math.max(req.start, opt.start),
// The earliest start time of this meeting
start: Math.max(req.start, opt.start),
label: `${req.name} - ${opt.name}`
});

const overlapRule = ({ overlap }) => overlap >= 30;
const sortLogic = (m1, m2) =>
// Sort by overlap first
m2.overlap > m1.overlap ? 1 :
m2.overlap < m1.overlap ? -1 :
// Sort by start second
m2.start > m1.start ? -1 :
m2.start < m1.start ? 1 :
0 ;

// Chain match, filter, sort:
const bestMatches = options
.map(match(requestByJohn))
.filter(overlapRule)
.sort(sortLogic);

console.log(
"Best match:",
bestMatches[0],
"\nAll possibilities:",
bestMatches
)
.as-console-wrapper { min-height: 100%; }

关于javascript - 如何从特定时间(例如上午 9 点到下午)以分钟为单位开始计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53298121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com