【问题标题】:PhantomJS Local ISO Date offPhantomJS 本地 ISO 日期关闭
【发布时间】:2017-12-26 19:59:19
【问题描述】:

我对 Angular 2 应用程序进行了一些测试,这些应用程序在 Firefox 中运行时通过但在 PhantomJS 中失败。在测试中,我正在检查日期是否设置正确。我以“2017-07-20T14:20”的形式输入本地 iso 日期字符串。日期部分设置正确,但时间已关闭。我在山区时区(-6:00 UTC)运行测试,所以时间是 08:20。是否有我可能缺少的垫片或设置配置。

这里有一些代码:

在我的 customDate 类文件中

public startTime: string;

public setDate(date: string) {
    let date:Date = new Date(date);
    this.startTime = date.getHours() + ':';
    if (date.getMinutes() < 10) {
        this.startTime += date.getMinutes() + '0';
    } else {
        this.startTime += date.getMinues();
    }
 }

在我的测试文件中

it('should pass') {
    let testDate = new customDate();
    testDate.setDate('2017-07-20T14:20');
    expect(testDate.startTime).toBe('14:00'); <-- this is failing it's comeing through as ('8:00')
}

【问题讨论】:

  • 我认为您输入的时间是UTC,除非您使用time offset

标签: angular jasmine phantomjs karma-runner


【解决方案1】:

在研究旧版 Chrome 和 Safari 将本地日期时间字符串解析为 UTC 的类似问题时,我们发现了以下解决方案:Javascript: parse a string to Date as LOCAL time zone。事实证明,PhantomJS 使用 ES5,因此日期时间字符串被解析为 UTC 而不是本地的。链接中的解决方案解释了发生了什么以及如何解决它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 2018-01-18
    • 2012-10-06
    • 1970-01-01
    相关资源
    最近更新 更多