js中时间格式转换和ajax中拼接时间格式转换

xiaoxiao2021-02-28  40

两种方法:

一种是使用velocity语言模板实现:

$!{date.format('yyyy-MM-dd hh:mm:ss',$!{bc.date})}

这种只适用于velocity模板语言,

第二种是js实现时间格式转换:

//js转换时间格式 Date.prototype.format = function(format) { var o = { "M+" : this.getMonth() + 1, // month "d+" : this.getDate(), // day "h+" : this.getHours(), // hour "m+" : this.getMinutes(), // minute "s+" : this.getSeconds(), // second "q+" : Math.floor((this.getMonth() + 3) / 3), // quarter "S" : this.getMilliseconds() // millisecond } if (/(y+)/.test(format)) { format = format.replace(RegExp.$1, (this.getFullYear() + "") .substr(4 - RegExp.$1.length)); } for ( var k in o) { if (new RegExp("(" + k + ")").test(format)) { format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)); } } return format; }

然后进行调用:

                    $.ajax({                         type:"post", url: url, data:{"comment":content,"codeReviewId":codeReviewId}, dataType: "json", success:function(data){ var dataObj = JSON.parse(data.content); var Datetemp= new Date(dataObj.createTime);// 这里必须是整数,毫秒 var dateStr = Datetemp.format("yyyy-MM-dd hh:mm:ss"); if(dataObj != ""){ var html=`<li class="media" style="margin-top:0px;"> <a class="pull-left" href="javascript:;"> <img class="todo-userpic" src= `" width="27px" height="27px"> </a> <div class="media-body todo-comment"> <p class="todo-comment-head" style="margin:7px 0"> <span class="todo-comment-username">`+dataObj.commentUserName+`</span>   <span class="todo-comment-date" style="color:#80809F">`+dateStr+`</span> </p> <p class="todo-text-color">`+dataObj.comment+`</p> </div> </li>` $("ul.comments").prepend(html); $("#commentBox").val("") } } }) })

转载请注明原文地址: https://www.6miu.com/read-2632922.html

最新回复(0)