微信小程序下订单件插(日历)

最近做小程序开发,出于练手,也是工作需要,就做了个微信小程序的类似于酒店预订的日历插件。

先上图:

这个插件分为上下两部分,上边是tab栏,会根据当前的日期自动定位到当前,并展示以后7天的日期,下边为内容展示,随tab栏变化而变化。

思路:
首先用new Data()时间对象初始化时间,获取当前的日期,用new Date(Date.UTC(year, month - 1, 1)).getDay()获取每个月的第一天是星期几。

// 计算每月第一天是星期几
function getFirstDayOfWeek(year, month) {
return new Date(Date.UTC(year, month - 1, 1)).getDay();
}
const date = new Date();
const cur_year = date.getFullYear();
const cur_month = date.getMonth() + 1;
const cur_date=date.getDate();
const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];

利用构造函数生成数据,一会用。

//利用构造函数创建对象
function calendar(date,week){
this.date=cur_year+'-'+cur_month+'-'+date;
if(date==cur_date){
this.week = "今天";
}else if(date==cur_date+1){
this.week = "明天";
}else{
this.week = '星期' + week;
}
}

使用for循环生成json数据:

for(var i=1;i<=monthLength;i++){
//当循环完一周后,初始化再次循环
if(x>6){
x=0;
}
//利用构造函数创建对象
that.data.calendar[i] = new calendar(i, [weeks_ch[x]][0])
x++;
}

这里因为一周有7天,所以当x>6的时候,重置为0。
最后展示部分源码

var that=this;
function getThisMonthDays(year, month) {
return new Date(year, month, 0).getDate();
}
// 计算每月第一天是星期几
function getFirstDayOfWeek(year, month) {
return new Date(Date.UTC(year, month - 1, 1)).getDay();
}
const date = new Date();
const cur_year = date.getFullYear();
const cur_month = date.getMonth() + 1;
const cur_date=date.getDate();
const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
//利用构造函数创建对象
function calendar(date,week){
this.date=cur_year+'-'+cur_month+'-'+date;
if(date==cur_date){
this.week = "今天";
}else if(date==cur_date+1){
this.week = "明天";
}else{
this.week = '星期' + week;
}
}
//当前月份的天数
var monthLength= getThisMonthDays(cur_year, cur_month)
//当前月份的第一天是星期几
var week = getFirstDayOfWeek(cur_year, cur_month)
var x = week;
for(var i=1;i<=monthLength;i++){
//当循环完一周后,初始化再次循环
if(x>6){
x=0;
}
//利用构造函数创建对象
that.data.calendar[i] = new calendar(i, [weeks_ch[x]][0])
x++;
}
//限制要渲染的日历数据天数为7天以内(用户体验)
var flag = that.data.calendar.splice(cur_date, that.data.calendar.length - cur_date <= 7 ? that.data.calendar.length:7)
that.setData({
calendar: flag
})
//设置scroll-view的子容器的宽度
that.setData({
width: 186 * parseInt(that.data.calendar.length - cur_date <= 7 ? that.data.calendar.length : 7)
})

最后奉上地址:github地址

作者:李佳明同学
链接:微信小程序下订单件插(日历)-教程-小程序社区-微信小程序-微信小程序开发社区-小程序开发论坛-微信小程序联盟
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 90天打卡累计天数:36/90 #父母的眼界决定孩子的世界# 孩子第一个30天目标: 21:30前完成睡前清单,关...
    魔奇行者阅读 845评论 0 0
  • 今天在澡堂里我无来由的想起了三毛,确切的说想到了她的《散哈拉的故事》里边她在非洲澡堂的奇遇。洗澡时搓澡在所...
    等爱的猫阅读 1,787评论 0 3
  • 著名作家余华在他的《兄弟》中写道:别的国家四百年的发展历程,我们国家却用了不到四十年就走完了。 这是一个急剧变化的...
    使者2阅读 3,998评论 0 1
  • 百天阅读营日志模板&片段 https://mubu.com/edit/TVqF2mt6G 自由组团打卡 https...
    萱草的时间阅读 1,267评论 0 0