【问题标题】:How to calculate finish date time in any timezone given the work hours needed and the Working Hours in PT timezone给定所需的工作时间和 PT 时区的工作时间,如何计算任何时区的完成日期时间
【发布时间】:2020-11-22 00:55:37
【问题描述】:

使用 JavaScript 和矩库,给定用户的现在、完成工作所需的小时数和分钟数,以及下表西雅图商店的营业和关闭时间。

太平洋时区的工作时间

我需要预测西雅图商店在用户所在时区完成工作的日期和时间。

谁能展示一个例子或建议一个算法来遵循?

编程语言无所谓,我可以翻译成JavaScript。

如果有使用'moment'库的例子就更好了。

非常感谢。

例子:

  1. 如果现在是太平洋时间星期一上午 8 点,并且工作需要 3 个小时,那么它将在太平洋时间星期一上午 11 点完成,因为它没有达到商店关门的时间。
  2. 如果工作需要 30 小时,则 (22-8= 14 ) 周一,2+4 = 6 关闭时间,(30-14) = 16 周二完成 = 周二下午 4 点在 PT(这是为了演示只有一个关门时间)。
  3. 如果用户在纽约,那么他的星期一早上 8 点,太平洋时间凌晨 4 点,将有 22 小时来完成星期一的 30 小时工作,2+4 = 6 关闭时间,(30-22) = 8 星期二太平洋时间上午 8 点是美国东部时间周二凌晨 4 点,因此对于纽约市用户来说,完成时间是周二凌晨 4 点。
  4. 长作业可以跨越多个关闭时间范围,周末可以增加更多。

我们在美国所有时区都有用户,可能很快在英国和德国都有用户,但在西雅图只有一家商店

【问题讨论】:

  • CloseTime 和 OpenHour 之间的差异会给你他们一天可以工作的小时数,对吧?所以周日 22 - 13 = 9 小时,对吗?
  • 是的。我应该称之为关闭时间。所以每天,他们工作的小时数是 closeTime -OpenHour
  • 打开/关闭时间与作业完成时间有何关系?也许你可以举一些输入和预期输出的例子?
  • 希尔顿 见上文。和评论一样,我很惊讶当你用谷歌搜索时,这样的东西并没有立即显示。 :-) 谢谢。

标签: momentjs moment-timezone luxon


【解决方案1】:

您要使用的算法是减法。从每天可用的持续时间列表开始。保留一个剩余持续时间的变量。找出开始日期剩余的小时数,然后从剩余的持续时间中减去它,或剩余的持续时间本身 - 以较少者为准。重复后续几天,直到剩余的持续时间为零。如果说在最后一天他们的工作日还剩下 4 小时,那么从他们一天的结束中减去这 4 小时,以获得他们完成工作的时间。

这可能有助于将所有内容分解为整数毫秒。将转换保留到时间戳直到结束。

【讨论】:

  • 但是您如何考虑可能的时区差异?商店始终位于 PT,但用户可以位于 ET、CT 或 MT。
  • 查看 momentjs api 文档,了解如何传递日期、小时、分钟和时区 (PT) 以将日期“解析”为时刻对象。然后你可以在你想要的任何时区“格式化”时刻对象。
【解决方案2】:

我想我明白了。 谁在乎使用 Luxon.JS 检查我的逻辑?

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Job Done with LuxonJS</title>
  <meta name="viewport" content="width=device-width">
  <style>
    #output {
      font-size: 2rem;
    }
    
    #output2 {
      font-size: 2rem;
      /* color: red; */
    }
    
    #output3 {
      font-size: 2rem;
      color: green;
    }
  </style>
</head>

<body>
  <header>
    <h1>Calculating Completion Date Time</h1>
  </header>
  <main>
    <div id="output"></div>
    <div id="output2"></div>
    <div id="output3"></div>
  </main>
  <!-- <script src="luxon.min.js"></script>-->
  <script src="https://cdn.jsdelivr.net/npm/luxon@1.24.1/build/global/luxon.min.js"></script>

  <script>
    Number.prototype.between = function(a, b, inclusive) { //For elegant If statment
      var min = Math.min.apply(Math, [a, b]),
        max = Math.max.apply(Math, [a, b]);
      return inclusive ? this >= min && this <= max : this > min && this < max;
    };
    console.clear();
    //weekday: number  Get the day of the week. 1 is Monday and 7 is Sunday
    var OpenClose = [{}, // elemnt 0 not used to make Luxon Day Of the Week the index into the array 
      {
        dw: 'Monday',
        open: 4,
        close: 22,
        LuxonDayOfWeek: 1
      },
      {
        dw: 'Tuesday',
        open: 4,
        close: 22,
        LuxonDayOfWeek: 2
      },
      {
        dw: 'Wedneday',
        open: 4,
        close: 22,
        LuxonDayOfWeek: 3
      },
      {
        dw: 'Thursday',
        open: 4,
        close: 22,
        LuxonDayOfWeek: 4
      },
      {
        dw: 'Friday',
        open: 4,
        close: 19,
        LuxonDayOfWeek: 5
      },
      {
        dw: 'Saturday',
        open: 6,
        close: 18,
        LuxonDayOfWeek: 6
      },
      {
        dw: 'Sunday',
        open: 13,
        close: 22,
        LuxonDayOfWeek: 7
      }
    ]; //orderby 
    console.log(OpenClose); //Show the Shop Schedule
    let DateTime = luxon.DateTime;
    var local = DateTime.local(); //Now here
    //Make changes here !!
    let Remainghours = 12.15; //Time alotted for the job. Change to test other values. 

    var NowInPT = local.setZone("America/Los_Angeles"); //Seattle is in this TimeZone
    let JobDonePT = NowInPT; //Start at same time
    output.textContent = "Starting in PT on " + NowInPT.toLocaleString(DateTime.DATETIME_SHORT) + " Job takes " + Remainghours + " hours";
    while (Remainghours > 0) {
      if (JobDonePT.hour.between(OpenClose[JobDonePT.weekday].open, OpenClose[JobDonePT.weekday].close, true)) {
        console.log("Shop Open: " + JobDonePT.toLocaleString(DateTime.DATETIME_SHORT) + " DW=" + JobDonePT.weekday + " " + Remainghours + " Hours outstanding");
        Remainghours--; //Shop is open Use up an hour
      } else {
        console.log("Shop Closed: " + JobDonePT.toLocaleString(DateTime.DATETIME_SHORT) + " DW=" + JobDonePT.weekday + " " + Remainghours + " Hours outstanding");
        //keep going without Using Remainghours 
      }
      JobDonePT = JobDonePT.plus({
        hours: 1
      }); //advance 1 hour on the calendar in any case
    }
    // Now we are left with a Negative fraction of an hour so we set set the actual Completion
    JobDonePT = JobDonePT.plus({
      hours: Remainghours
    }); //Remainghours is negative
    //The end DateTime may still be in Off hours Must end In work hours so
    while (!JobDonePT.hour.between(OpenClose[JobDonePT.weekday].open, OpenClose[JobDonePT.weekday].close, true)) { //Not working hours
      debugger;
      JobDonePT = JobDonePT.plus({
        hours: 1
      });
    };
    output2.textContent = "Job End in PT on " + JobDonePT.toLocaleString(DateTime.DATETIME_SHORT);
    var JobDoneMylocal = JobDonePT.toLocal();
    output3.textContent = "Job End in My Time Zone on " + JobDoneMylocal.toLocaleString(DateTime.DATETIME_SHORT);
  </script>
</body>

</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    相关资源
    最近更新 更多