直接上formatTime.js
const date = new Date();let y = date.getFullYear() //年let m = date.getMonth() + 1 //月let d = date.getDate() //日m = m < 10 ? '0' + m : m //月份补 0d = d < 10 ? '0' + d : d //日数补 0//当月月第一天function monthFirst(){return [y, m, 1].join("-")}//获取当月最后一日function monthLast(){let lastD = new Date(y, m, 0).getDate()return [y, m, lastD].join("-")}//本周第一天function weekFirt(){let week = date.getDay() //星期if (week === 0) {week = 7}let weekFirstDay = new Date(date - (week - 1) * 86400000)return farmatT(weekFirstDay)}// 本周最后一天function weekLast(){let week = date.getDay() //星期if (week === 0) {week = 7}var weekFirstDay = new Date(date - (week - 1) * 86400000)var weekLastDay = new Date((weekFirstDay / 1000 + 6 * 86400) * 1000)return farmatT(weekLastDay)}//中国标准时间转为为 yy-mm-ddconst farmatT = time=>{let date = new Date(time);let y = date.getFullYear();let m = date.getMonth() + 1;m = m < 10 ? ('0' + m) : m;let d = date.getDate();d = d < 10 ? ('0' + d) : d;// let h = date.getHours();// let minute = date.getMinutes();// minute = minute < 10 ? ('0' + minute) : minute;return y + '-' + m + '-' + d}module.exports = {monthFirst,monthLast,weekFirt,weekLast}
在小程序的js页面引入
constformatTime=require('formatTime.js');
调用
console.log(formatTime.weekFirt()); //调用console.log(formatTime.weekLat()); console.log(formatTime.monthFirst()); console.log(formatTime.monthLast());
如果觉得《js 获取本月/本周第一天和最后一天 封装并在小程序引用》对你有帮助,请点赞、收藏,并留下你的观点哦!