Home  >  Article  >  Web Front-end  >  How to solve the format of ajax unit seconds when parsing json data

How to solve the format of ajax unit seconds when parsing json data

php中世界最好的语言
php中世界最好的语言Original
2017-12-04 11:53:322087browse


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 ? (&#39;0&#39; + m) : m;
       var d = date.getDate();
       d = d < 10 ? (&#39;0&#39; + d) : d;
       var h = date.getHours();
       h = h < 10 ? (&#39;0&#39; + h) : h;
       var minute = date.getMinutes();
       var second = date.getSeconds();
       minute = minute < 10 ? (&#39;0&#39; + minute) : minute;
       second = second < 10 ? (&#39;0&#39; + second) : second;
       return y + &#39;-&#39; + m + &#39;-&#39; + d + &#39; &#39; + h + &#39;:&#39; + minute + &#39;:&#39; + 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn