时间的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>
显示: