【问题标题】:Getting d.getTime is not a function获取 d.getTime 不是函数
【发布时间】:2018-11-20 10:14:11
【问题描述】:

我正在努力获取时间戳的日期值。我想获得日期的 UTC 等效值,因为当前的工作实现使用我的本地时区。我发现了这个非常酷的方法toUTCString()

给定以下代码行:

console.log("new Date(data[i].time).toUTCString()" , new Date(data[i].time).toUTCString());
console.log("new Date(data[i].time)" , new Date(data[i].time))
tempData.push(Object.assign({}, data[i], {'date': new Date(data[i].time).toUTCString()}));

控制台日志返回以下内容,我得到转换后的结果:

new Date(data[i].time).toUTCString() Wed, 06 Jun 2018 03:50:00 GMT
new Date(data[i].time) Wed Jun 06 2018 11:50:00 GMT+0800 (+08)

给我这个错误信息:

Uncaught (in promise) TypeError: d.getTime is not a function

作为进一步的研究,一些 SO 答案指出问题在于它是一个字符串值,但我认为我使用正确的方法将其转换为 UTC。

我在这里遗漏了什么吗?谢谢!

我在运行 webpack 时自动生成了一个文件,这是我根据错误指示的行。

var discontinuousIndexCalculator = (0, _utils.slidingWindow)().windowSize(2).undefinedValue(function (d, idx, di) {
    var i = di;
    var row = {
        date: d.getTime(),
        startOf30Seconds: false,
        startOfMinute: false,
        startOf5Minutes: false,
        startOf15Minutes: false,
        startOf30Minutes: false,
        startOfHour: false,
        startOfEighthOfADay: false,
        startOfQuarterDay: false,
        startOfHalfDay: false,
        startOfDay: true,
        startOfWeek: false,
        startOfMonth: false,
        startOfQuarter: false,
        startOfYear: false
    };
    var level = evaluateLevel(row, d, i);
    return _extends({}, row, { index: i }, level);
});

【问题讨论】:

  • 你在哪里使用getTime()
  • 我认为这只是 d 不是 Date 对象
  • 我没有在我的代码中使用getTime(),但我会将上面的代码条粘贴到我找到它的地方。
  • @JohnReyTanquinco Nop。你在使用任何外部库吗?看起来您正在使用一个外部库,该库期望数据/参数/配置中的日期对象,而您不仅仅传递日期..?
  • 我正在使用这个实现github.com/rrag/react-stockcharts-examples2/blob/master/…,但我在这里找到了我的自定义实际代码pastebin.com/7TY0utp3。并且没有使用常量parseDate

标签: javascript function date timestamp utc


【解决方案1】:

您可以尝试类似的方法。

const data = [
 {
  time:new Date().getTime()
 },
 {
  time:new Date().getTime()
 },
 {
  time:new Date().getTime()
 },
 {
  time:new Date().getTime()
 },
 {
  time:new Date().getTime()
 }
];

const dataWithString = [];

for(let i = 0; i< data.length;i++){
   console.log(new Date(data[i].time).toUTCString()); //Something similar has you have
   
   //Solution 1 create an other array and save the index to map each array;
   dataWithString.push({
    dateString:new Date(data[i].time).toUTCString(),
    index:i
  });
  
  //Solution 2
  data[i].dateString = new Date(data[i].time).toUTCString();
}

console.log("Results");
console.log(dataWithString);
console.log(data);

【讨论】:

  • 我在解决方案 1 中得到 Cannot read property 'getTime' of undefined,而在解决方案 2 中得到相同的先前错误。作为观察,我在这里认为,我的代码的实现需要像这样的日期对象 @987654323 @ 而不是由toUTCString() 方法生成的字符串"Wed, 06 Jun 2018 03:50:00 GMT"。嗯,我不确定,但有没有办法可以将 UTC 字符串值转换回日期对象?
  • 是的,new Date("UTC String");
【解决方案2】:

在答案和蒂姆的评论here 中找到了我需要的东西。可能不是最好的解决方案,但这有效。这实际上给了我一个我需要的 Date 对象,而不是一个字符串值。

我使用:new Date(new Date().toUTCString().substr(0, 25));

【讨论】:

    猜你喜欢
    • 2021-09-29
    • 2022-01-03
    • 2016-12-19
    • 2018-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 2021-07-13
    相关资源
    最近更新 更多