Js日期-Unix时间戳错误的结果

问题描述 投票:0回答:1

嗨,我已经使用在线生成器生成了Unix时间戳,然后选择了该日期:

01/12/2020 @ 9:30pm (UTC)

哪个给了我结果:1578864600

我现在想在我的应用程序中显示它:(日期是我上面的结果)

const timestamp = new Date().setMilliseconds(date);
  const formattedDate = new Intl.DateTimeFormat('pl-PL', {
    year: 'numeric',
    month: 'numeric',
    day: 'numeric',
    weekday: 'long',
    hour: 'numeric',
    minute: 'numeric',
  }).format(timestamp);

但是在我的应用程序中,我得到了错误的日期:

Tuesday, 14.01.2020, 17:07

为什么计算错了?

javascript reactjs unix-timestamp
1个回答
0
投票

Unix时间戳是从1970年1月1日开始的秒数。您正在使用当前日期创建一个新的Javascript DateTime对象,然后使用setMilliseconds()函数添加几天。要将Unix时间戳转换为Javascript DateTime,请检查以下堆栈溢出答案:from unix timestamp to datetime

© www.soinside.com 2019 - 2024. All rights reserved.