When ajax parses json data, date appears in seconds (1511352532000)

1. Description of the problem:

When parsing json data through ajax on a jsp page, it is found that date-type data is displayed in seconds, similar to 1511352532000.

2. Analysis:

The format 1511352532000 is a millisecond format for date-type data, which means that it is a problem with the display format of the data, because the background is to directly convert the queried object to json, as follows:

    @ResponseBody//Get product information that contains the page breaks
    @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);
    }

3. Resolution:

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;
    }

Call the function directly where you need to convert the format, and don't forget to pass in a parameter of type date.
The following:

...
trObj += "<td width='20px'>" + fmtDate(page.content[i].createDate) + "</td>";
...

4. Result display:

Front

after

5. Write at the end:

But are we all lost stars.Trying to light up the dark~

Tags: JSON JSP Session

Posted on Sun, 19 Jul 2020 12:23:03 -0400 by taskhill