在微信小程序或者web项目中经常用到需要获取当前时间往后多少天并显示日期含星期几的需求,现在优化为只调用一个js函数,只需调用getDates(days)函数,传入需要显示多少天日期,即返回一个携带日期的数组。
utils/util 程序
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
}
//判断两个时间比较大小
function compareDate(d1, d2) {
return ((new Date(d1.replace(/-/g, "\/"))) > (new Date(d2.replace(/-/g, "\/"))));
}
//当前时间获取
function getCurrentToday(){
const date = new Date()
var year = date.getFullYear();
var mouths = (date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : (date.getMonth() + 1);
var day = date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate();
var hours = date.getHours();
var Minutes = date.getMinutes();//当前分
var currentdate = year + "-" + mouths + "-" + day + "\t" + hours + ":" + Minutes;
return currentdate
}
/**
* 传入时间后几天
* param:传入时间:dates:"2018-04-02",later:往后多少天
*/
function dateLater(dates, later) {
let dateObj = {};
let show_day = new Array('周日', '周一', '周二', '周三', '周四', '周五', '周六');
let date = new Date(dates);
date.setDate(date.getDate() + later);
let day = date.getDay();
dateObj.dates = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1) + "月" + (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate()) + "日";
dateObj.newdates = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1) + "-" + (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
dateObj.year = date.getFullYear();
dateObj.month = ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1);
dateObj.day = (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
dateObj.week = show_day[day];
return dateObj;
}
module.exports = {
formatTime: formatTime,
compareDate: compareDate,
getCurrentToday: getCurrentToday,
dateLater: dateLater
}
js程序
var util = require('../../utils/util.js');
Page({
data: {
list:[] //存储日期列表
},
onLoad: function () {
console.log(this.getCurrentMonthFirst())
// console.log(this.getDates(7))
this.setData({
list:this.getDates(10) //获取10天后日期
})
console.log(this.data.list)
},
//获取当前时间
getCurrentMonthFirst: function () {
var date = new Date();
var todate = date.getFullYear() + "-" + ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth() + 1) + "-" + (date.getDate() < 10 ? ("0" + date.getDate()) : date.getDate());
return todate;
},
getDates: function (days, todate = this.getCurrentMonthFirst()) {
var dateArry = [];
for (var i = 0; i < days; i++) {
var dateObj = util.dateLater(todate, i);
dateArry.push(dateObj)
}
return dateArry;
},
})
wxml程序
<view wx:for="{{list}}">
<button>{{item.dates}} {{item.week}}</button>
</view>
参考链接:
微信小程序获取日期天数带星期 https://www.jianshu.com/p/33736c93d716
具体应用实例请看github上面的日期选择组件栗子:https://github.com/kingbuwu/weapp-date.git