【问题标题】:Converting Object retrieve from API into DateTime Format将从 API 检索的对象转换为 DateTime 格式
【发布时间】:2021-06-22 22:09:28
【问题描述】:

这是我使用 API 从 sharepoint 调用的数据

object datatype

然后我尝试将对象转换为日期时间格式。但它返回 NaN 值

console log

这是我尝试过的代码

.subscribe((dataChart) => {
    console.log(dataChart)
    console.log(typeof(dataChart))

    var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
    var str=dataChart.strRequestDate;   //Set the string in the proper format(best to use ISO format ie YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS)
    var d=new Date(str);  //converts the string into date object
    var m=d.getMonth(); //get the value of month
    console.log(m) // Print the month name
      
    },

谁能指出我的错误

编辑: 根据@N.F 说明,在我控制台登录 e 后出现项目列表

new log

【问题讨论】:

    标签: angular typescript datetime object type-conversion


    【解决方案1】:

    dataChart 看起来是数组。您必须使用for 循环来获取每个元素。

    .subscribe((dataChart) => {
        console.log(dataChart);
        console.log(typeof(dataChart));
    
        var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
        
        for (const e of dataChart) {
          var str=e.strRequestDate;   //Set the string in the proper format(best to use ISO format ie YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS)
          var d=new Date(str);  //converts the string into date object
          var m=d.getMonth(); //get the value of month
          console.log(m) // Print the month name
        
        }
          
      },
    

    【讨论】:

    • 我运行循环。这次它返回未定义的 401 项。这是什么意思?
    • console.log(m) 更改为console.log(str) 时看到什么?
    • 还是一样的 401 undefined item
    • 那你看到console.log(e);
    • 我已经更新了我的问题。控制台日志中出现项目列表
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 2021-11-06
    • 1970-01-01
    • 2012-10-17
    相关资源
    最近更新 更多