简体   繁体   English

在日期 Object (JavaScript) 中插入时间戳时,日期 output 错误

[英]Wrong Date output when inserting timestamp in Date Object (JavaScript)

I am trying to use data from the https://www.cryptocompare.com/ api.我正在尝试使用来自https://www.cryptocompare.com/api的数据。 Weard thing is, when I want to create a new Date Object with that timestamp, I get Tue Jan 20 1970 07:09:15 GMT+0100 (Mitteleuropäische Normalzeit) back.磨损的是,当我想用那个时间戳创建一个新的 Date Object 时,我得到 Tue Jan 20 1970 07:09:15 GMT+0100 (Mitteleuropäische Normalzeit) 回来。 The timestamp is right tho, i checked that on https://timestampgenerator.com/date-from-timestamp .时间戳是正确的,我在https://timestampgenerator.com/date-from-timestamp上检查了它。

requestLatestNews();
    function requestLatestNews() {
        fetch(`https://min-api.cryptocompare.com/data/v2/news/?lang=EN&api_key=<key>`)
            .then((response) => {
                console.log(response);
                return response.json();
            })
            .then((data) => {
                console.log(data);
                return data;
            })
            .then((data) => {
                for (let i = 0; i < data.Data.length; i++) {
                    let publishDate = new Date(data.Data[i].published_on); // Falsche Berechnung
                    console.log(publishDate);
                    let dmy = publishDate.getUTCDate() + "/" + (publishDate.getUTCMonth() + 1) + "/" + publishDate.getUTCFullYear();
                    let hms = publishDate.getUTCHours() + ":" + publishDate.getUTCMinutes() + ":" + publishDate.getUTCSeconds();
                    document.querySelector('#output').innerHTML
                        += `<p> <a href="${data.Data[i].guid}">${data.Data[i].source}</a> - ${data.Data[i].title}, ${dmy} ${hms}</p>
                    <img src="${data.Data[i].imageurl}"></img> <p> [${data.Data[i].tags} ${data.Data[i].categories}] </p>
                    <p> ${data.Data[i].body} </p>`;
                }
            })
            .catch((error) => {
                console.log('Error Code: ' + error);
            });
    }

I think you should take a look at the Date contructor for JavaScript: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date我认为您应该查看 JavaScript 的日期构造函数: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date

Quoting from the documentation:从文档中引用:

Time value or timestamp number - value时间值或时间戳编号 - 值

An integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC (the ECMAScript epoch, equivalent to the UNIX epoch), with leap seconds ignored.一个 integer 值,表示自 1970 年 1 月 1 日 00:00:00 UTC(ECMAScript 纪元,相当于 UNIX 纪元)以来的毫秒数,忽略闰秒。 Keep in mind that most UNIX Timestamp functions are only accurate to the nearest second.请记住,大多数 UNIX 时间戳功能仅精确到最接近的秒。

You can try out the value you receive on that page as well.您也可以尝试在该页面上收到的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM