1. 获取本周日期

微信小程序 常用函数_i++

note:上周日到这周六的日期

result: getWeeks()接收参数为时间格式,返回的是关于日期的一个数组 [2019-02-17,2019-02-18,…]

getDates() 返回日期的数组[17,18,19…]

const UTIL = require('../../../utils/util.js');
getDates() {
let nowDate = new Date()
let timesStamp = nowDate.getTime();
let currenDay = nowDate.getDay();
let dates = [];
// 减去的一是为了获取包括上周日的时间数组
for (var i = 0; i < 7; i++) {
dates.push(new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - (currenDay + 6) % 7 - 1)).getDate());
}
return dates
},
getWeeks(nowDate) {
let nowDate = new Date()
let timesStamp = nowDate.getTime();
let currenDay = nowDate.getDay();
let weekDates = [];
for (var i = 0; i < 7; i++) {
weekDates.push(UTIL.timeStampToYMD(timesStamp + 24 * 60 * 60 * 1000 * (i - (currenDay + 6) % 7 - 1)));
}
return weekDates
},

引用中的timeStampToYMD时间戳转化成yyyy-mm-dd格式函数:

const timeStampToYMD = timeStamp => {
var date = new Date(timeStamp);
return formatTime(date).slice(0, formatTime(date).indexOf(' ')).replace(/\//g, '-');
}
const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()

return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
module.exports = {
formatTime: formatTime,
timeStampToYMD: timeStampToYMD
}

2. 时间戳转化成年月日、分时秒

const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()

return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
// 时间戳转化成 年月日
const timeStampToYMD = timeStamp => {
var date = new Date(timeStamp);
return formatTime(date).slice(0, formatTime(date).indexOf(' ')).replace(/\//g, '-');
}
// 时间戳转化成 分时秒
const timeStampToHMS = timeStamp => {
var date = new Date(timeStamp);
return formatTime(date).slice(formatTime(date).indexOf(' ') + 1);
}

2. checkbox 限制最多选择的个数

project 数组结构 为 [{“value”:“购物”;“isSelected”:false},{“value”:“逛街”;“isSelected”:false}]

<checkbox-group wx:for="{{project}}" wx:key="{{index}}" bindchange="checkboxChange" data-index='{{index}}'>
<checkbox value='{{item.value}}' disabled="{{isMoreItem && !item.isSelected}}">{{item.value}}</checkbox>
</checkbox-group>

isMoreItem初始值为false,当 totalNum 中选中的数组个数大于5个时,islimitItem为true 则新的未选择的不可选。

// 选中
checkboxChange(e) {
let project = this.data.project;
let index = e.currentTarget.dataset.index;
project[index].isSelected = !project[index].isSelected;
this.setData({
project: project
})
let totalNum = project.filter((value)=>{
return value.isSelected
}).length

if (totalNum > 4) {
wx.showToast({
title: '最多不能超过5项',
icon: 'none'
})
this.setData({
islimitItem: true
})
} else {
this.setData({
islimitItem: false
})
}
console.log("totalNum*****", totalNum, "islimitItem", this.data.islimitItem)
},

4. //判断已经出生多少年、多少月、多少日

接收一个数组: birthday 格式为 2019-03-11

婴儿[0,2) 儿童 [2,12) 成人12以上 return 3,2,1

judgeAge(birthday) {
let ageObj = {};
let date = birthday.replace(/-/g, '/'); // 字符转化 兼容IOS
let birth = new Date(`${date} 00:00:00`) //拼接时间
let now = new Date()
let yearDiff = now.getFullYear() - birth.getFullYear()
let monthDiff = now.getMonth() - birth.getMonth()
let dayDiff = now.getDate() - birth.getDate()
if (birth.getTime() > now.getTime()) {
return false;
}
// 婴儿[0,2) 儿童 [2,12) 成人[12以上 return 3,2,1
const babyYear = 2
const adultYear = 12
if (yearDiff == babyYear) {
return this.currentDateResult(monthDiff, dayDiff, 3, 2)
} else if (yearDiff == adultYear) {
return this.currentDateResult(monthDiff, dayDiff, 2, 1)
} else if (yearDiff > babyYear && yearDiff < adultYear) {
return 2;
} else {
return yearDiff < babyYear ? 3 : yearDiff > adultYear ? 1 : 2;
}
},
currentDateResult(monthDiff, dayDiff, oneResult, twoResult) {
if (monthDiff == 0) {
return dayDiff < 0 ? oneResult : twoResult;
} else {
return monthDiff < 0 ? oneResult : twoResult;
}
},

5. 小数点保留 位数

toFixed() 使用的是银行家舍入规则:四舍六入五取偶(又称四舍六入五留双)。

(0.015).toFixed(2); // 0.01 ,

// 保留n位小数并格式化输出(不足的部分补0)
var fomatFloat = function(value, n) {
var f = Math.round(value*Math.pow(10,n))/Math.pow(10,n);
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
s += '.';
}
for(var i = s.length - s.indexOf('.'); i <= n; i++){
s += "0";
}
return s;
}
//var num3 = fomatFloat(0.015, 2); // 0.02

6. 清除为undefined的空数组

// 移除未定义的数组
removeEmptyArrayEle(arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == undefined) {
arr.splice(i, 1);
i = i - 1;
}
}
return arr
},