如何使用jQuery获取数组中的日期?

时间:2019-05-18 07:25:06

标签: php jquery ajax getdate

我试图从json获取日期值。但是日期值在数组内部。

代码如下

get.php

<?php
    include_once('connection.php');

    $staff_number   = $_POST['staff_number'];

    $query      = "SELECT * FROM TABLE.VW_LIST WHERE STAFFNUMBER=?";  
    $stmt       = sqlsrv_prepare( $connection, $query, array(&$staff_number));
    $result     = sqlsrv_execute($stmt);
    $row        = sqlsrv_fetch_array($stmt);    
    echo json_encode($row);
?>

输出如下所示

{
    0: "1", 
    1: "test", 
    2: "name", 
    3: "001", 
    4: "12345", 
    5: {
            date: "2019-05-18 00:00:00.000000", timezone_type: 3, timezone: "Asia/Singapore"\
       }, 
    6: "ABC123"
}

问题是当我尝试在ajax中显示它时,它在html界面中显示了 [object,Object]

在PHP代码中,我知道该怎么做

date.php

$displaydate = $row['DISPLAYDATE'];
foreach($displaydate as $date)
{
}
$old = date('Y-m-d H:i:s', strtotime($displaydate['date']));
$output = new DateTime($old);
echo $output->format('d M Y');

但是在Ajax / jQuery中,我不确定该怎么做。这是我的代码

display.php

<script>
    $(document).ready(function(){       
        var staff_number = "";
        $('#pick').change(function(){               
            staff_number = $(this).val();
            $.ajax({
                url: "get.php",
                type: "POST",
                dataType: "json",
                data: {"staff_number": staff_number},
                success: function(data)
                {
                    var ar = data;  
                    var displaydate     = ar[5];
                    $('#displaydate').val(displaydate); 
                },
                error: function(error){
                     console.log("Error:");
                     console.log(error);
                }
            });
        });
    });
</script>

如何获取日期值?

先谢谢了。

1 个答案:

答案 0 :(得分:0)

您还必须调用名为date的属性

 var displaydate = ar[5].date;