【问题标题】:change datetime format of datatable.js from ex. (Oct 24 2017 06:08:53:000AM) to (2017-10-24 06:08:53.000)从 ex. 更改 datatable.js 的日期时间格式。 (2017 年 10 月 24 日 06:08:53:000AM) 至 (2017-10-24 06:08:53.000)
【发布时间】:2017-10-24 00:50:35
【问题描述】:

Oct 24 2017 06:08:53:000AM - (datatable.js 的默认输出)

2017-10-24 06:08:53.000 - 这是我从 SQL Server 中提取的内容

我希望输出与我从 SQL 服务器获得的相同

Sample Screenshot of DataTable.js output

Sample Screenshot of SQL server output

我正在使用此代码来调用表格

<script>
$(document).ready(function(){
$('#myTable').dataTable({
    });
});
</script>
</html>  

但是格式会自动更改,我正在尝试搜索一些信息,如何更改日期时间格式以与我在 SQL Server 中获得的格式相同。

datatable.js 的 HTML 代码

<table id="myTable" class="table table-striped" >  
                <thead>  
                    <tr>  
                        <th>Mobile Number</th>  
                        <th>First Name</th>  
                        <th>Last Name</th>  
                        <th>DateTime</th>  
                        <th>Message</th>  
                    </tr>  
                </thead>  

                <tbody>  
                    <?php 
                    $z = 0; 
                    $x = 0;
                    $query_qmobile = "select distinct "
                    . "phone_book.s_mobile_number as 'Mobile Number', "
                    . "phone_book.s_first_name as 'First Name', "
                    . "phone_book.s_last_name as 'Last Name', "
                    . "inbox.dt_datetime as 'Date', "
                    . "inbox.s_message as 'Message' "
                    . "from phone_book, inbox "
                    . "where phone_book.s_mobile_number = inbox.s_sender "
                    . "order by dt_datetime DESC";

                    $result_qmobile = mssql_query($query_qmobile);
                    $numRows_qmobile = mssql_num_rows($result_qmobile); //TOTAL ROW COUNTS

                    $data_q4 = array();
                    while($row=mssql_fetch_assoc($result_qmobile)){
                    $data_q4[]= $row;
                    }

                    for($x;$x<$numRows_qmobile;$x++){
                    echo '<tr>';
                    echo '<td> '.$data_q4[$z]['Mobile Number'].' </td>';
                    echo '<td> '.$data_q4[$z]['First Name'].' </td>';
                    echo '<td> '.$data_q4[$z]['Last Name'].' </td>';
                    echo '<td> '.$data_q4[$z]['Date'].' </td>'; //THIS IS THE PART THAT CONVERT THE DATA
                    echo '<td> '.$data_q4[$z]['Message'].' </td>';
                    echo '</tr>';
                    $z++;
                    }
                    ?>
                </tbody>  
            </table> 

【问题讨论】:

    标签: javascript sql-server twitter-bootstrap datatables-1.10


    【解决方案1】:

    这应该可以解决问题

    document.write(ConvertDate("Oct 24 2017 06:08:53:000AM"));
    
    function ConvertDate(string){
    	var date = {
          	  month : "",
              day : "",
              year : "",
              hours : "",
              minutes : "",
              seconds : "",
              milliseconds : "",
              period : ""
          };
          
    	  var dateArr = string.split(" ");
          var timeArr = dateArr[3].split(":");
          
          date.month = function () {
                // a dirty trick to get the numeric value
                var value = new Date(dateArr[0] + " 1").getMonth()
          			return value < 10 ? "0" + value : value;
          }();
          date.day = dateArr[1];
          date.year = dateArr[2];
          date.hours = timeArr[0];
          date.minutes = timeArr[1];
          date.seconds = timeArr[2];
          date.milliseconds = timeArr[3].substring(0, 3);
          date.period = timeArr[3].replace(date.milliseconds, "");
          
          return date.year+"-"+date.month+"-"+date.day+" "+(date.period == "PM" ? date.hours + 12 : date.hours)+":"+date.minutes+":"+date.seconds+"."+date.milliseconds;
    }

    然后形成jquery

    $("#myTable <some selector for your date time column>").each(function(){
         $(this).html(ConvertDate($(this).html()));
    
    }); 
    

    更新

    -CHANGE 
    
    echo '<td> '.$data_q4[$z]['Date'].' </td>'; to 
    echo '<td class=\"dates\"> '.$data_q4[$z]['Date'].' </td>';
    
    -THEN
    
    $("#myTable .dates").each(function(){
         $(this).html(ConvertDate($(this).html()));    
    }); 
    

    【讨论】:

    • 嗨@Nick 我该怎么做这部分
    • 给你的日期时间列一个类名。将 替换为该类名。如果您使用表格的源代码更新您的问题,我可能会更具体。
    • 完美运行。谢谢@尼克
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 2018-05-21
    相关资源
    最近更新 更多