We know that when parsing json data through ajax on a jsp page, we sometimes find that date type data is displayed in the form of seconds, so how do we convert this parsing result? Woolen cloth? Let’s take a look together. The format
1511352532000 is the millisecond format of date type data. This means that it is a problem with the display format of the data. Since the background directly converts the object obtained by query into json, as follows :
@ResponseBody//获取包含了分页后的产品信息 @RequestMapping(value = "/userSelect/paging", produces = "text/html;charset=UTF-8") public String userSelectPaging(String goPage, HttpSession session) { int page; if (goPage.equals("")) page = 0; else page = Integer.parseInt(goPage); Sort sort = new Sort(Sort.Direction.DESC, "createDate"); Pageable pageable = new PageRequest(page, 10, sort); Page<user> users = userService.findAll(pageable, session); return JSON.toJSONString(users, true); }</user>
3. Solution:
Write a js function on the jsp page, as follows
function fmtDate(inputTime) { var date = new Date(inputTime); var y = date.getFullYear(); var m = date.getMonth() + 1; m = m < 10 ? ('0' + m) : m; var d = date.getDate(); d = d < 10 ? ('0' + d) : d; var h = date.getHours(); h = h < 10 ? ('0' + h) : h; var minute = date.getMinutes(); var second = date.getSeconds(); minute = minute < 10 ? ('0' + minute) : minute; second = second < 10 ? ('0' + second) : second; return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second; }
Just call the function directly where the format needs to be converted, don’t forget You need to pass in a date type parameter~~~
is as follows:
... trObj += "" + fmtDate(page.content[i].createDate) + ""; ...
I believe you have mastered the method after reading these cases. For more exciting content, please pay attention to php Chinese website Other related articles!
Related reading:
Implementation steps of DOM programming in html5
Using h5 to make WeChat payment process Implementation steps
#Use canvas to make a clock implementation steps
The above is the detailed content of How to solve the format of ajax unit seconds when parsing json data. For more information, please follow other related articles on the PHP Chinese website!