JavaScript JSON响应 - 格式化日期和时间

时间:2017-03-31 07:21:04

标签: javascript json date time cakephp-3.0

当我触发我的JavaScript代码以打开模态时,会发送AJAX调用以检索JSON格式的数据。然后我使用JSON响应来填充我的模态。

触发JavaScript的链接是:

<?= $this->Html->link(__('View'), ['action' => 'popup', $session->id], ['class' => 'view', 'data-id' => $session->id]) ?>

这是CakePHP 3 View上的表格中的一个动作。 $ session-&gt; id是根据点击链接的哪一行数据决定的。 Popup是一个空的CakePHP 3函数,它只是简化了JavaScript的工作和模式的开放。

触发模态的JavaScript如下:

<script type="text/javascript">
    $(function () {
        $('.view').click(function (ev) {
            ev.preventDefault();
            var sessionId = $(this).attr('data-id');
            $('#viewModal').modal('show');
            $.ajax({
                url:"localhost/project/sessions/details/"+sessionId+".json",
                type:'POST',
                success:function(res) {
                    if(res) {
                        document.getElementById("prdatestart").innerHTML = res.date_start;
                        document.getElementById("prstarttime").innerHTML = res.starttime;
                    }
                }
            });
        });
    });
</script>

我的CakePHP 3 SessionsController的详细功能就是这个,它检索之前获得的$ session-&gt; id的相关数据:

public function details($id = null)
    {
        $details = $this->Sessions->get($id);
        $this->set(array(
            'output' => $details,
            '_serialize' => 'output',
            '_jsonp' => true
        ));
    }

这是我的模式,然后打开:

<!-- Modal -->
<div class="modal fade" id="viewModal" tabindex="-1" role="dialog" aria-labelledby="viewModalLabel"
     aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h3 class="modal-title" id="viewModalLabel">Session Details</h3>
                <br>
                <table class="vertical table col-sm-2">
                    <tr>
                        <th>Starting Date:</th>
                        <td id="prdatestart"></td>
                    </tr>
                    <tr">
                        <th>Starting Time:</th>
                        <td id="prstarttime"></td>
                    </tr>
                </table>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close
                </button>
            </div>
        </div>
    </div>
</div>

但是,我的日期和时间采用以下格式:

  • 日期:2017-05-12T00:00:00 + 00:00
  • 时间:2017-03-31T00:14:00 + 11:00

对于日期响应,我只需要日期,并以D / M / Y格式格式化。对于时间响应,我只需要时间,格式为12小时hh:mm AM / PM格式。 (我最后不需要时区内容,因为在首次提交数据时会考虑到这一点。)

1 个答案:

答案 0 :(得分:1)

只需使用普通的javascript new Date() .post就像简单的js函数cal(yourdatevarible ,whichtype) type=date|time

var date = '2017-05-12T00:00:00+00:00';
var time = '2017-03-31T00:14:00+11:00';

console.log(cal(date, 'date'))
console.log(cal(time, 'time'))

function cal(date, type) {

  var m = ['Jan', 'Feb', 'Mar', 'Aprl', 'May', 'Jun', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  //var w =['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
  var x = new Date(date)
  var h = x.getHours() > 12 ? x.getHours() - 12 : x.getHours();
  var format = x.getHours() > 12 ? 'PM' : 'AM';
  if (type == 'date') {
    return x.getDate() + ' ' + m[x.getMonth()] + ' ' + x.getFullYear();
  } else {
    var min = x.getMinutes().toString().length > 1 ? x.getMinutes() : '0' + x.getMinutes();
     h =h.toString().length > 1 ? h : '0' + h;
    return h + ':' + min + ' ' + format;
  }
}