在不知道确切的时间戳/天的情况下找到日期差异

发布于 2025-02-12 14:49:34 字数 263 浏览 2 评论 0原文

我发现自己有点复杂。我需要找到两天的时间戳在毫秒内的时间戳,而不会预先定义任何一个。我所知道的是,我需要一个小的时间戳才能成为现在,我想我可以使用date.now()(如果我错了,请更正我)。其他时间戳可以是一周中的任何一天,我无法控制用户选择的地方。

所以我想要这样的事情:

var dif = The future thursday - now

我只知道言语的日子。我不知道如何将其更改为毫秒的时间戳。

I have found my self in a bit of complication. I need to find the difference between two days timestamps' in milliseconds without predefining any of them. All I know is that I need the small timestamp to be now which I guess I can get by using Date.now() (correct me if I'm wrong). The other timestamp can be any day of the week and I have no control over which the user picks.

So I want something like this:

var dif = The future thursday - now

I only know the days in words. I do not know how to then change that to timestamp in milliseconds.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狠疯拽 2025-02-19 14:49:34

您可以通过算术从一周的另一天开始获得一周中特定一天的下一个实例。然后,您可以在特定日期开始时获得该日期和午夜之间的区别,例如

// Get next instance of particular weekday.
// ECMAScript day numbering, i.e. 0 = Sunday, 1 = Monday, etc.
// If targetDay == currentDay, get next instance of day
function getNextWeekday(targetDay, date = new Date()) {
  let d = new Date(date);
  // For targetDay, make Sunday 7 not 0
  targetDay = targetDay || 7;
  d.setDate(
    d.getDate() + (((targetDay - d.getDay() + 7) % 7) || 7)
  );
  return d;
}

// Get time difference in milliseconds between
// 00:00 on the targetDay from 00:00 on supplied date.
function getTimeDiff(targetDay, date = new Date()) {
  let d = getNextWeekday(targetDay, date);
  return d.setHours(0,0,0,0) - new Date(date).setHours(0,0,0,0);
}

// The following might return fractional days were the
// calculation crosses a DST boundary
// Get timeDiff to next Sunday
let timeDiff = getTimeDiff(0);
console.log(`${timeDiff} (${timeDiff/8.64e7} days)`);

You can get the next instance of a particular day of the week from another day of the week by arithmetic. You can then get the difference between that date and midnight at the start of a specific day, e.g.

// Get next instance of particular weekday.
// ECMAScript day numbering, i.e. 0 = Sunday, 1 = Monday, etc.
// If targetDay == currentDay, get next instance of day
function getNextWeekday(targetDay, date = new Date()) {
  let d = new Date(date);
  // For targetDay, make Sunday 7 not 0
  targetDay = targetDay || 7;
  d.setDate(
    d.getDate() + (((targetDay - d.getDay() + 7) % 7) || 7)
  );
  return d;
}

// Get time difference in milliseconds between
// 00:00 on the targetDay from 00:00 on supplied date.
function getTimeDiff(targetDay, date = new Date()) {
  let d = getNextWeekday(targetDay, date);
  return d.setHours(0,0,0,0) - new Date(date).setHours(0,0,0,0);
}

// The following might return fractional days were the
// calculation crosses a DST boundary
// Get timeDiff to next Sunday
let timeDiff = getTimeDiff(0);
console.log(`${timeDiff} (${timeDiff/8.64e7} days)`);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文