嗨,我有一个5号角项目。我在根据用户时区显示日期时遇到问题。
这是我的示例代码段。
formatDateForTableDisplay(dateValueAsString: string) {
console.log(dateValueAsString); // this will print for example 2020-05-31
const dateObject = new Date(dateValueAsString);
if (this.formGroup.get('frequency').value === 'PER_DAY') {
return this.datePipe.transform(dateObject, 'mediumDate');
} else if (this.formGroup.get('frequency').value === 'PER_MONTH') {
return this.datePipe.transform(dateObject, 'LLL, yyyy');
} else {
return this.datePipe.transform(dateObject, 'yyyy');
}
}
问题是想象如果我们将日期字符串作为2020-05-31传递给函数formatDateForTableDisplay。在另一个用户中,时区可能是6月1日,因为用户时区比格林尼治标准时间+8小时,因此需要打印为2020-06-01。我该怎么实现
谢谢
答案 0 :(得分:0)
您有dateValueAsString
进入2020-05-31,这意味着您没有小时,分钟或秒(在这种情况下,小时是最重要的)。因此,您无法确定dateValueAsString
是在凌晨2点保存的(意味着它将保留到2020-05-31)还是在11pm被保存(意味着应该转换为2020-06-01)>