JSON Date Format - Display Time Only

I have an application using Bootstrap tables, and one of the fields is Time. When I get data from the database, it is encoded in JSON format, the data for this field is about the same as 2016-11-07T13: 40: 29.000Z, which I understand is the standard JSON format.

I would like to break it into pieces, and one column in my table will display the date, and the other column will display the time ideally. But I would be happy if I could just get the "Time" column to display only the time.

I read that this has something to do with adding a dataFormatter to the column header, but I can't get it to work, since my javascript returns NaN.

This is the code I found while researching the problem. I am new to Javascript, so there are probably some errors here that I would really like to get.

<table id="table"  data-url ="http://maccdx161012:4567/api/v1/sat" data-toggle="table">
    <thead>
        <tr>

           <th data-field="initials">Initials</th>
            <th data-field="sector">Sector</th>
            <th data-field="cjs">CJS</th>
            <th data-field="satin" data-formatter="timeFormatter">In</th> 
            <th data-field="satout">Out</th> 
            <th data-field="duration">Duration</th> 
            <th data-field="position">Position</th>
            <th data-field="ot">OT</th>             
        </tr>
    </thead>
 </table>
<script>
    function timeFormatter(value) {
        var date = new Date(value*1000);
        var hours = date.getHours();
        var minutes = "0" + date.getMinutes();

        return hours + ':' + minutes.substr(-2);
    }
</script>
+4
source share
1 answer
<script>
    function timeFormatter(value) {
        var date = new Date(value);
        var hours = date.getHours();
        var minutes = leadZero(date.getMinutes());

        return hours + ':' + minutes;
    }
    function dateFormatter(value) {
        var date = new Date(value);
        var years = date.getFullYear();
        var months = leadZero(date.getMonth() + 1);
        var days = leadZero(date.getDate());

        return years + '-' + months + '-' + days;
    }
    function leadZero(n) { return n>9 ? n : "0" + n; }
</script>

It will return the time for your local time zone. If you do not want this feature, use the Keith solution.

+2
source

Source: https://habr.com/ru/post/1660550/


All Articles