1.在pages里新建文件夹common,common里新建.js文件命名method.js
如何获取当前时间
- 时间少一个月需要注意的是,当前月份倒流回上个月份,倒流时间已上个有多少天为准
- 三月判断上一个月的天数需要考虑闰年和平年的天数
- 当前时间的时间戳 - 上个月的天数×24小时×60分钟×60秒×1000毫秒
<template><view style&#61;"margin: 50rpx;"><text>一个月前&#xff1a;</text><text>{{da.year }} {{da.time}}</text></view>
</template>
export default {data() {return {da:&#39;&#39;,now:&#39;&#39;}},onShow() {var end &#61; Date.now()var day &#61; 30var enda &#61; new Date(end);var endm &#61; enda.getMonth()&#43;1;var d &#61; enda.getDate();if(d &#61;&#61; 31){day &#61; 31}if(endm&#61;&#61;1 || endm&#61;&#61;2 || endm&#61;&#61;4 || endm&#61;&#61;6 || endm&#61;&#61;8 || endm&#61;&#61;9 || endm&#61;&#61;11){day&#61;31}if(endm &#61;&#61; 3){var endy &#61; enda.getFullYear();day &#61; 28if(d &#61;&#61; 31){day &#61; 31}if(d &#61;&#61; 30){day &#61; 30}if(d &#61;&#61; 29){day &#61; 29}if(endy % 4 &#61;&#61; 0){day &#61; 29if(d &#61;&#61; 31){day &#61; 31}if(d &#61;&#61; 30){day &#61; 30}if(d &#61;&#61; 29){day &#61; 29}}}var start &#61; 1000*60*60*24*dayvar interval &#61; end - start;this.da &#61; this.getTime(interval)},methods: {getTime(e){var a &#61;new Date(e);var y&#61;a.getFullYear();var m&#61;a.getMonth()&#43;1;m &#61; m<10? "0"&#43;m:m;var d&#61;a.getDate();d &#61; d<10? "0"&#43;d:d;var h&#61;a.getHours();h &#61; h<10? "0"&#43;h:h;var mm&#61;a.getMinutes();mm &#61; mm<10? "0"&#43;mm:mm;var s&#61;a.getSeconds();s &#61; s<10? "0"&#43;s:s;return {year : y &#43; &#39;/&#39; &#43; m &#43; &#39;/&#39; &#43; d,time : h &#43; &#39;:&#39; &#43; mm &#43; &#39;:&#39; &#43; s,hm : h &#43; &#39;:&#39; &#43; mm,h : h,mm : mm,s : s,y : y,m : m,d : d} }}
完成&#xff01;