【发布时间】: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 时间戳的末尾?
以下是引用的类似问题...
【问题讨论】:
-
在
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