0

我从 JSON 中的后端接收 LocalDate 女巫看起来像

{"dayOfMonth":25,
 "dayOfWeek":"TUESDAY",
 "dayOfYear":206,
 "month":"JULY",
 "monthValue":7,
 "year":2017,
 "hour":0,
 "minute":0,
 "nano":0,
 "second":0,
 "chronology":{"id":"ISO","calendarType":"iso8601"}}}

我想把它解析成一个时刻 js 对象

 moment(this.car.overview).format();

失效日期

console.log(moment().format(car.overview));

错误类型错误:format.replace 不是函数

任何人都知道如何从此 JSON 中获取有效的时刻对象?

4

2 回答 2

0

你需要做的是

 moment().minute(this.car.minute.overview.minute).second(this.car.minute.overview.second).hours(this.car.minute.overview.hour) 

等等信息阅读https://momentjs.com/docs/#/get-set/

于 2017-09-19T07:32:12.913 回答
0

你是这个意思?

PS:如果你收到一个 JSON 字符串,你需要先 JSON.parse 它

var overview = {
  "dayOfMonth": 25,
  "dayOfWeek": "TUESDAY",
  "dayOfYear": 206,
  "month": "JULY",
  "monthValue": 7,
  "year": 2017,
  "hour": 0,
  "minute": 0,
  "nano": 0,
  "second": 0,
  "chronology": {
    "id": "ISO",
    "calendarType": "iso8601"
  }
}

console.log(
  moment({
    y: overview.year,
    M: overview.monthValue - 1,
    d: overview.dayOfMonth,
    h: overview.hour,
    m: overview.minute,
    s: overview.second,
    ms: overview.nano
  })
)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

于 2017-09-19T07:53:09.487 回答