PhantomJS 本地 ISO 日期关闭
PhantomJS Local ISO Date off
我对 Angular 2 应用程序进行了一些测试,该应用程序在 Firefox 中 运行 时通过,但在 PhantomJS 中失败。在测试中,我正在检查日期设置是否正确。我以“2017-07-20T14:20”的形式输入本地 iso 日期字符串。日期部分设置正确但时间已关闭。我 运行 在山区时区 (-6:00 UTC) 进行测试,所以时间是 08:20。是否有我可能缺少的垫片或设置配置。
这是一些代码:
在我的自定义日期 class 文件中
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')
}
在研究旧版本 Chrome 和 Safari 将本地日期时间字符串解析为 UTC 的类似问题时,我们找到了以下解决方案:。事实证明,PhantomJS 使用 ES5,因此日期时间字符串被解析为 UTC 而不是本地。 link 中的解决方案解释了发生了什么以及如何解决它。
我对 Angular 2 应用程序进行了一些测试,该应用程序在 Firefox 中 运行 时通过,但在 PhantomJS 中失败。在测试中,我正在检查日期设置是否正确。我以“2017-07-20T14:20”的形式输入本地 iso 日期字符串。日期部分设置正确但时间已关闭。我 运行 在山区时区 (-6:00 UTC) 进行测试,所以时间是 08:20。是否有我可能缺少的垫片或设置配置。
这是一些代码:
在我的自定义日期 class 文件中
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')
}
在研究旧版本 Chrome 和 Safari 将本地日期时间字符串解析为 UTC 的类似问题时,我们找到了以下解决方案: