问题描述
我有这个代码块,例如,我从API那里收到了ScheduledTime和timezone 预定时间:1603890000000 时区:“ EDT”
,我想在用户设备的时区(即CST)中显示此内容。最好的解决方法是什么?
<p className="content mb-0 mt-2" style={{color:"#868686"}}>
{moment(new Date(workout.scheduledTime)).format("hh:mm A")}{" to "}
{moment(new Date(workout.scheduledTime)).add(workout.duration,"minutes").format("hh:mm A")}{" "}
</p>
解决方法
使用toLocale()*
函数族在正确的语言环境中显示日期
const theDate = new Date( Date.parse('10/28/2020 4:52:48 PM UTC'));
console.log(theDate.toLocaleString())
console.log(theDate.toLocaleTimeString());
console.log(theDate.toLocaleDateString());