将LocalDate json解析为Angular中的时刻

时间:2017-09-19 07:21:35

标签: json angular momentjs localdate

我在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));
  

错误TypeError:format.replace不是函数

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

2 个答案:

答案 0 :(得分: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/

答案 1 :(得分: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>
&#13;
&#13;
&#13;