【问题标题】:new Date().getTime() showing NaN in JavaScriptnew Date().getTime() 在 JavaScript 中显示 NaN
【发布时间】:2020-08-23 07:38:31
【问题描述】:

我正在尝试使用new Date.getTime() 将当前时间保存在时间戳键内的临时对象中,并将其推送到全局可访问变量GLOBAL_VAR 内的数组中。 但是在我创建临时变量并打印它的值后,它显示时间戳是NaN 我不知道为什么我不能在对象中获取它(console.log(),结果如下所示)

另外, handleOrderMessage函数在收到socket.io事件时调用不知道会不会影响new Date.getTime()函数,显示输出:

console.log("TEMP OBJECT IS", tempObject)
/*
{
id: "-909e-11ea-9ede-066a42ffe1ae",
​price: "0.1",
​quantity: "0.1",
​side: "buy",
​timestamp: NaN
}
*/
    function handleOrderMessage(msg) {


        let d = new Date();
        console.log(d.getTime());
        if (msg['status'] === "PLACED") {
            //for buy
            if (msg['orderSide'] === "Buy") {
                let tempObject = {
                    side: "buy",
                    id: msg["OrderID"],
                    timestamp: d.getTime(),
                    quantity: parseFloat(msg["Quantity"].toFixed(4)).toString(),
                    price: parseFloat(msg["Price"].toFixed(4)).toString()
                }
                //pushing to the global orders with price from msg as key
                if (tempObject.price in GLOBAL_VAR.bidOrdersPricesAsKey) {
                    GLOBAL_VAR.bidOrdersPricesAsKey[tempObject.price].push(tempObject);
                } else {
                    GLOBAL_VAR.bidOrdersPricesAsKey[tempObject.price] = [tempObject]
                }
                console.log("GLOBAL VAR BUY ORDERS PRICES ASK KEY", GLOBAL_VAR.bidOrdersPricesAsKey)
                console.log("TEMP OBJECT IS", tempObject)
      }
    }

【问题讨论】:

  • 你的 sn-p 很难用。您应该添加必要的变量和函数调用。
  • ...并删除所有不需要展示您的案例的内容
  • 在这里工作正常:jsfiddle.net/7qgpcda1/1
  • 这是一个非常大的文件..所以我没有展示它

标签: javascript html nan gettime


【解决方案1】:

以下似乎工作正常,不知道为什么你会得到 NaN。请提供完整的工作代码来解决问题。

doit();
function doit() {
  let d = new Date();
  let tempObject = {
    side: "buy",
    timestamp: d.getTime()
  };
  console.log(tempObject);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-26
    • 2013-04-10
    • 1970-01-01
    • 2020-01-12
    • 1970-01-01
    • 2016-07-14
    相关资源
    最近更新 更多