评论

收藏

[jQuery] jQuery数据结构渲染(2):时间戳的处理

开发技术 开发技术 发布于:2021-07-30 19:08 | 阅读数:514 | 评论:0

时间的json数据格式:data.json
{"commitTime": 1588061853944}
示例代码:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
  <head>
    <title>jQuery数据结构渲染(2):时间戳的处理</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
  </head>
  <body>
    时间:<div id="commintTime"></div>
  </body>
  <script type="text/javascript">
    $.ajax({
      url: "data.json",
      type: 'GET',
      dataType: 'json',
      success: function(data) {
        //
        var htm = '';
        htm += '' + formatTime(data.commintTime) + '';
        $('#commintTime').html(htm)
      }
    });
    //格式化时间,时间戳的处理
    function formatTime(commintTime) {
      var date = new Date();
      //date.setTime(value);
      var month = date.getMonth() + 1;
      var hours = date.getHours();
      if (hours < 10)
        hours = "0" + hours;
      var minutes = date.getMinutes();
      if (minutes < 10)
        minutes = "0" + minutes;
      var time = date.getFullYear() + "-" + month + "-" + date.getDate() +
        " " + hours + ":" + minutes;
      return time;
    }
  </script>
</html>
显示:

DSC0000.png



关注下面的标签,发现更多相似文章