微信小程序关于处理时间的工具类

1 篇文章 0 订阅
1 篇文章 0 订阅

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 formatMoney = money => {
let arr = (money.toFixed(2)+’’).split(’.’);
let target = arr[0];
let result = “”;
while(target > 1000){
result = target%1000 + ‘,’ + result;
target = Math.floor(target/1000);
}
result = target%1000 + ‘,’ + result;
result = result.substring(0, result.length-1) + “.” + arr[1];
return result;
}

const formatTimeYMD = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()

return [year, month, day].map(formatNumber).join(’-’)
}

const formatTimeMD = date => {
const month = date.getMonth() + 1
const day = date.getDate()
return month+"."+day
}

const formatYearMonth = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
return year+‘年’+month+‘月’
}

//获取传入时间本月的天数 type=0 本月 type=1 上月
const getMonthDaysNumber = (time, type) => {
const date = new Date(time);
const nowYear = date.getFullYear()
const nowMonth = date.getMonth() + 1
let preYear, preMonth;
if(type == 0){
if(nowMonth + 1 > 12){
preYear = nowYear + 1;
preMonth = 1;
} else{
preYear = nowYear;
preMonth = nowMonth + 1;
}
}else if(type == 1){
if(nowMonth - 1 < 1){
preYear = nowYear - 1;
preMonth = 12;
} else{
preYear = nowYear;
preMonth = nowMonth - 1;
}
}
let timestamp = Math.abs(new Date(preYear+’/’+preMonth+’/1 00:00:00’).getTime() - new Date(nowYear+’/’+nowMonth+’/1 00:00:00’).getTime());
let daysNumber = timestamp/1000/60/60/24;
return daysNumber;
}

//获取传入当月的天数是星期几
const getNowMonthWitchWeek = (time,day) => {
const date = new Date(time);
const nowYear = date.getFullYear()
const nowMonth = date.getMonth() + 1
let resultWeek = new Date(nowYear+’/’+nowMonth+’/’+day+’ 00:00:00’).getDay();
resultWeek = (resultWeek == 0?7:resultWeek);
return resultWeek;
}

module.exports = {
formatTime: formatTime,
formatMoney: formatMoney,
formatNumber: formatNumber,
formatTimeYMD: formatTimeYMD,
formatTimeMD: formatTimeMD,
formatYearMonth: formatYearMonth,
getMonthDaysNumber: getMonthDaysNumber,
getNowMonthWitchWeek: getNowMonthWitchWeek
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值