【问题标题】:RRule UNTIL date not being saved correctly with timezoneRRule UNTIL 日期未与时区正确保存
【发布时间】:2021-04-19 20:46:38
【问题描述】:

我在 VueJS 中使用 RRule 包 (akubroztocil/rrule) 的 JS 版本。 我注意到,在使用toString() 方法创建 RRule 字符串时,UNTIL 时间戳不包括末尾的“Z”。我相信这是为了表明时区?例如。我将 UNTIL 日期解析为 Date 对象,然后将其转换为 UTC,如下所示:

let rrule = {};

//this.until = "2021-05-01" for example
const parsedDate = new Date(Date.parse(this.until)); 
const until = new Date(
  Date.UTC(
    parsedDate.getFullYear(),
    parsedDate.getMonth(),
    parsedDate.getDate()
  )
);
this.$set(rrule, "until", until);
this.rule = new RRule(rrule);

我对 DTSTART 执行上述完全相同的操作。如果我然后运行this.rule.toString() 方法,结果字符串是:DTSTART:20210419T000000Z\nRRULE:UNTIL=20210501T000000;FREQ=WEEKLY;BYDAY=MO,FR;INTERVAL=1

注意 DTSTART 末尾有一个“Z”,但 UNTIL 没有。这会导致我的 Laravel 后端出现异常,我使用的是同一 RRule 包的 PHP 版本。如果我手动将“Z”添加到末尾,那么它可以正常工作。

后端的异常:

"Invalid UNTIL property: if the "DTSTART" property is specified as a date with UTC time or a date with local time and time zone reference, then the UNTIL rule part MUST be specified as a date with UTC time."

我怎样才能做到不必手动将“Z”添加到 UNTIL 时间戳的末尾?

以下是引用的类似问题...

Timezone issue with UNTIL in ical RRULE statement

【问题讨论】:

  • new Date(Date.parse(this.until)) 中使用Date.parse 是多余的,new Date(this.until) 将产生相同的结果。格式为 YYYY-MM-DD 的时间戳被解析为 UTC。 new Date( Date.UTC(...)) 部分使用本地年、月和日值并将它们视为 UTC,有效地将具有 -ve 时区偏移量的主机的日期向后移动一天(注意 ECMAScript 偏移量与通常使用的偏移量具有相反的意义,所以 + ve ECMAScript 偏移量)。

标签: javascript vue.js date datetime rrule


【解决方案1】:

我通过删除之前在我忘记的规则集上设置的规则解决了这个问题。

this.$set(rule, "tzid", "UTC");

根据这个问题: https://github.com/jakubroztocil/rrule/issues/440

"如果没有指定 tzid,它只会添加 Z"

在明确删除 UTC tzid 后,它会按预期生成 RRule 字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 2011-12-25
    • 2021-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多