1、首先,创建一个wxs的文件,内容如下
var formatTime = function(date) {
var date = getDate(date); //返回当前时间对象
var year = date.getFullYear()
var month = fixz(date.getMonth() + 1)
var day = fixz(date.getDate())
var hour = fixz(date.getHours())
var minute = fixz(date.getMinutes())
var second = fixz(date.getSeconds())
return [year, month, day].join(’-’) + ’ ’ + [hour, minute, second].join(’:’)
}
var fixz = function(num) {
if (num < 10) {
return ‘0’ + num
}
return num
}
module.exports = {
formatTime: formatTime
}
2、在wxml中引用文件
<wxs module=‘tools’ src=‘tools.wxs’></wxs>
3、在wxml中转格式
{{tools.formatTime(item.createTime)}}
写在最后,为什么不直接用外部的js?因为所有的所有的数据需要提前在page下的js中处理好才能输出到wxml中,比较麻烦。用wxs可以直接在页面中转化,而且可以直接复用。
请问你这个方法如何让当前时间减去8个小时呢?
var date=function(n,t){var r,e,u=["Sun","Mon","Tues","Wednes","Thurs","Fri","Satur","January","February","March","April","May","June","July","August","September","October","November","December"],o=/\\?(.?)/gi,i=function(n,t){return e[n]?e[n]():t},c=function(n,t){for(n=String(n);n.length<t;)n="0"+n;return n};e={d:function(){return c(e.j(),2)},D:function(){return e.l().slice(0,3)},j:function(){return r.getDate()},l:function(){return u[e.w()]+"day"},N:function(){return e.w()||7},S:function(){var n=e.j(),t=n%10;return t<=3&&1===parseInt(n%100/10,10)&&(t=0),["st","nd","rd"][t-1]||"th"},w:function(){return r.getDay()},z:function(){var n=new Date(e.Y(),e.n()-1,e.j()),t=new Date(e.Y(),0,1);return Math.round((n-t)/864e5)},W:function(){var n=new Date(e.Y(),e.n()-1,e.j()-e.N()+3),t=new Date(n.getFullYear(),0,4);return c(1+Math.round((n-t)/864e5/7),2)},F:function(){return u[6+e.n()]},m:function(){return c(e.n(),2)},M:function(){return e.F().slice(0,3)},n:function(){return r.getMonth()+1},t:function(){return new Date(e.Y(),e.n(),0).getDate()},L:function(){var n=e.Y();return n%4==0&n%100!=0|n%400==0},o:function(){var n=e.n(),t=e.W();return e.Y()+(12===n&&t<9?1:1===n&&9<t?-1:0)},Y:function(){return r.getFullYear()},y:function(){return e.Y().toString().slice(-2)},a:function(){return 11<r.getHours()?"pm":"am"},A:function(){return e.a().toUpperCase()},B:function(){var n=3600*r.getUTCHours(),t=60*r.getUTCMinutes(),e=r.getUTCSeconds();return c(Math.floor((n+t+e+3600)/86.4)%1e3,3)},g:function(){return e.G()%12||12},G:function(){return r.getHours()},h:function(){return c(e.g(),2)},H:function(){return c(e.G(),2)},i:function(){return c(r.getMinutes(),2)},s:function(){return c(r.getSeconds(),2)},u:function(){return c(1e3*r.getMilliseconds(),6)},e:function(){throw new Error("Not supported (see source code of date() for timezone on how to add support)")},I:function(){return new Date(e.Y(),0)-Date.UTC(e.Y(),0)!=new Date(e.Y(),6)-Date.UTC(e.Y(),6)?1:0},O:function(){var n=r.getTimezoneOffset(),t=Math.abs(n);return(0<n?"-":"+")+c(100*Math.floor(t/60)+t%60,4)},P:function(){var n=e.O();return n.substr(0,3)+":"+n.substr(3,2)},T:function(){return"UTC"},Z:function(){return 60*-r.getTimezoneOffset()},c:function(){return"Y-m-d\\TH:i:sP".replace(o,i)},r:function(){return"D, d M Y H:i:s O".replace(o,i)},U:function(){return r/1e3|0}};var a,f;return a=n,r=void 0===(f=t)?new Date:f instanceof Date?new Date(f):new Date(1e3*f),a.replace(o,i)};
console.log(date("Y-m-d H:i:s u"));