uni-app 怎么获取时间,获取年月日,获取星期,解决小程序在苹果手机少了八个小时
1.在pages里新建文件夹common,common里新建.js文件命名method.js
这里结合导出方法一起用,如果单纯在页面用方法的话,复制代码进自定义方法里即可
function yearTime(e){
var a = new Date();
var y=a.getFullYear();
var m=a.getMonth()+1;
m = m<10? "0"+m:m;
var d=a.getDate();
d = d<10? "0"+d:d;
var h=a.getHours();
h = h<10? "0"+h:h;
var mm=a.getMinutes();
mm = mm<10? "0"+mm:mm;
var s=a.getSeconds();
s = s<10? "0"+s:s;
if(e == 'year'){
return y + '/' + m + '/' + d;
}
if(e == 'time'){
return h + ':' + mm + ':' + s;
}
if(e == 'yearTime'){
return y + '/' + m + '/' + d + ' ' + h + ':' + mm + ':' + s;
}
return y + '/' + m + '/' + d;
}
function week(){
var h=a.getDay()-1;
var we=['星期一','星期二','星期三','星期四','星期五','星期六','星期七']
return we[h]
}
function appleTime(){
// 苹果时间
var end = Date.now()
var start = 1000*60*60*8
var interval = end + start; //结束 - 开始 = 毫秒值
var a =new Date(interval);
var y=a.getFullYear();
var m=a.getMonth()+1;
m = m<10? "0"+m:m;
var d=a.getDate();
d = d<10? "0"+d:d;
var h=a.getHours();
h = h<10? "0"+h:h;
var mm=a.getMinutes();
mm = mm<10? "0"+mm:mm;
var s=a.getSeconds();
s = s<10? "0"+s:s;
return y + '/' + m + '/' + d + ' ' + h + ':' + mm + ':' + s;
}
module.exports = {
yearTime : yearTime,
week : week,
appleTime : appleTime
}
2.在页面里引用方法
<script>
import {
yearTime
} from '@/pages/common/all.js'
import {
appleTime
} from '@/pages/common/all.js'
export default {
mounted() {
this.da.year = yearTime('year')
this.da.time = yearTime('time')
this.da.yearTime = yearTime('yearTime')
this.da.appleTime = appleTime()
console.log(this.da)
},
data() {
return {
da: {
year: '',
time: '',
yearTime: '',
appleTime: ''
}
}
},
methods: {
}
}
</script>
3.获取到的时间
完成,既学会了方法导出,又学会了获取时间的方法