【问题标题】:Move dates from week to week in moment js在时刻js中将日期从一周移动到一周
【发布时间】:2019-08-26 08:53:14
【问题描述】:

我正在制定一个在线时间表,允许用户将某个时刻从一周中的特定时间转移到另一周。使用 MomentJS,我想知道是否有现成的解决方案,或者想办法实现这一点。

这是一个用例示例:

  1. 用户有 2018 年 12 月 28 日星期五的约会
  2. 用户想要在 2 周后移动它
  3. 预期结果是 2019 年 1 月 11 日星期五

如果有帮助,这里是该情况的基本代码(在 TypeScript 中):

let firstAppointment = moment("2018-12-28T00:00:00.000Z");
let amountOfWeek:number = 2;
//do the operation...

编辑 1 我已经进行了一些测试,这是结果。结果日期好像差了几个小时

let firstAppointment = moment("2018-12-28T00:03:00.000Z");
firstAppointment.add(2,"weeks");
console.log(firstAppointment.toISOString(false));
//result: 2019-01-10T19:03:00.000Z

这可能与时区有关吗?

【问题讨论】:

  • 遗憾的时区总是有点棘手。尝试在所有日期中使用 UTC

标签: typescript momentjs


【解决方案1】:

感谢 Alex Kucksdorf,这是解决方案。为了计算正确的时间,我们需要进入由 MomentJS 提供的 UTC 模式 Moment.js UTC documentation

let firstAppointment = moment.utc("2018-12-28T00:03:00.000Z");
firstAppointment.add(2,"weeks");
console.log(firstAppointment.toISOString());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多