以角度格式化日期对象,如何

乐趣

在获取请求后,我得到了这种类型的 json 格式:

{
"name" : "cow",
"date" : {
    "year" : 2012,
    "month" : "JANUARY",
    "dayOfMonth" : 1,
    "dayOfWeek" : "FRIDAY",
    "dayOfYear" : 1,
    "monthValue" : 1,
    "hour" : 2,
    "minute" : 2,
    "second" : 0,
    "nano" : 0,
    "chronology" : {
      "id" : "ISO",
      "calendarType" : "iso8601"
    }
  }
....
}

如何在打字稿/角度中将上述内容转换为以下内容:

{
"name" : "cow",
"date" : "2012-01-01"
}

然后存储在下面的数组中。

//....correctFormat = [];
service.convertStore()
  .subscribe((data: any) => {
    // manipulation here

    correctFormat = "holds result"
})
迈汉尼加特

访问每个成员,将其分配给一个新对象并将其存储在数组中:

const object = {
  "name": "cow",
  "date": {
    "year": 2012,
    "month": "JANUARY",
    "dayOfMonth": 1,
    "dayOfWeek": "FRIDAY",
    "dayOfYear": 1,
    "monthValue": 1,
    "hour": 2,
    "minute": 2,
    "second": 0,
    "nano": 0,
    "chronology": {
      "id": "ISO",
      "calendarType": "iso8601"
    }
  }
}

const d = object.date
correctFormat = [];
correctFormat.push({
  name: object.name,
  date: d.year + '-' + (0 + '' + d.monthValue).slice(-2) + '-' + (0 + '' + d.dayOfMonth).slice(-2)
});

console.log(correctFormat)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章