在php中以文本格式显示日期时间

时间:2017-05-22 11:06:56

标签: php ajax

  

我想以2017年3月3日T 07:30:00

的格式显示日期时间

这是我的剧本

 <script>
            $(document).ready(function () {
            var from = "";
            $('#loadings').show();
            $.ajax({
                type: "GET",
                url: 'http://apis.fdf.df/api/Get_Loadsheet_Details/<?php 
                echo $number; ?>',
                dataType: 'json',
                success: function (response) {
                    $('#loadings').hide();
                    console.log(response);                        
                    document.getElementById('ldate').innerHTML = 
                    response[0].DATE;
                    }, error: function (errors) {
                    console.log(errors);//alert('hi');                    
                    $('#loadings').hide();
                    $('#error').html("<h2><span style='color:red;'>No data 
                    found on this LR No.</span></h2>");                     
                }
                });
                });
             </script>
  

我api的控制台输出是

[{"DATE":"2017-04-03T00:00:00","lrConfirmStatus":null,"lrLoadStatus":null}]
  

我希望将其绑定到id为&#34; ldate&#34;的字段。

                     <tr>
                        <td>LOAD  DATE:</td>
                        <td id="ldate"> </td>
                    </tr>

2 个答案:

答案 0 :(得分:2)

您需要一个格式化日期的功能。您想要的formatDate函数格式日期:

<script>
    $(document).ready(function () {
        var from = "";
        $('#loadings').show();
        $.ajax({
            type: "GET",
            url: 'http://apis.fdf.df/api/Get_Loadsheet_Details/<?php 
            echo $number; ?>',
            dataType: 'json',
            success: function (response) {
                $('#loadings').hide();
                console.log(response);                        
                document.getElementById('ldate').innerHTML = formatDate(response[0].DATE);
            }, 
            error: function (errors) {
                console.log(errors);//alert('hi');                    
                $('#loadings').hide();
                $('#error').html("<h2><span style='color:red;'>No data found on this LR No.</span></h2>");                     
            }
        });
    });

    function formatDate(date_string) {
        var date = new Date(date_string);
        var monthNames = [
        "January", "February", "March",
        "April", "May", "June", "July",
        "August", "September", "October",
        "November", "December"
        ];

        var day = date.getDate();
        var monthIndex = date.getMonth();
        var year = date.getFullYear();

        if(day < 10) {
            day = '0'+day;
        }

        return  monthNames[monthIndex] + ' ' + day  + ', ' + year + ' T ' + date.getHours()+':' + date.getMinutes() +':' +date.getSeconds();
    }
</script>

答案 1 :(得分:0)

您需要在成功中添加以下内容

$("#ldate").append(response[0]["DATE"])