首页  >  文章  >  web前端  >  JavaScript实现每日签到功能

JavaScript实现每日签到功能

青灯夜游
青灯夜游转载
2018-10-10 15:17:274673浏览

本文给大家介绍JavaScript实现每日签到功能,有一定的参考价值,有需要的朋友可以参考一下,希望对你们有所帮助。

先看一下效果图:

1.jpg

var calUtil = {  
    getDaysInmonth : function(iMonth, iYear){ //当前年月的总天数 
      var dPrevDate = new Date(iYear, iMonth, 0);  
      return dPrevDate.getDate();  
    },  
    bulidCal : function(iYear, iMonth) { //构建当前年月对应的日历
      var aMonth = new Array();  
      aMonth[0] = new Array(7);  
      aMonth[1] = new Array(7);  
      aMonth[2] = new Array(7);  
      aMonth[3] = new Array(7);  
      aMonth[4] = new Array(7);  
      aMonth[5] = new Array(7);  
      aMonth[6] = new Array(7);  
      var dCalDate = new Date(iYear, iMonth - 1, 1);  
      var iDayOfFirst = dCalDate.getDay();  
      var iDaysInMonth = calUtil.getDaysInmonth(iMonth, iYear);  
      var iVarDate = 1;  
      var d, w;  
      aMonth[0][0] = "日";  
      aMonth[0][1] = "一";  
      aMonth[0][2] = "二";  
      aMonth[0][3] = "三";  
      aMonth[0][4] = "四";  
      aMonth[0][5] = "五";  
      aMonth[0][6] = "六";  
      for (d = iDayOfFirst; d < 7; d++) {  
        aMonth[1][d] = iVarDate;  
        iVarDate++;  
      }  
      for (w = 2; w < 7; w++) {  
        for (d = 0; d < 7; d++) {  
          if (iVarDate <= iDaysInMonth) {  
            aMonth[w][d] = iVarDate;  
            iVarDate++;  
          }  
        }  
      }  
      return aMonth;  
    },  
    ifHasSigned : function(signList,day){  
      var signed = false;  
      $.each(signList,function(index,item){  
          var date = new Date(item.signDate);  
          if(date.getDate() == day) {  
              signed = true;  
              return false;  
          }  
      });  
      return signed ;  
    },  
    drawCal : function(iYear, iMonth ,signList) {  
      var currentYearMonth = iYear+"年"+iMonth+"月";  
      var myMonth = calUtil.bulidCal(iYear, iMonth);  
      var htmls = new Array();  
      htmls.push("

"); htmls.push("

"); htmls.push("

"+currentYearMonth+"

"); htmls.push("

"); htmls.push("

"); htmls.push("

"); htmls.push(""); htmls.push(""); htmls.push(""); htmls.push(""); htmls.push(""); htmls.push(""); htmls.push(""); htmls.push(""); htmls.push(""); var d, w; for (w = 1; w < 7; w++) { htmls.push(""); for (d = 0; d < 7; d++) { var ifHasSigned = calUtil.ifHasSigned(signList,myMonth[w][d]); if(ifHasSigned){ htmls.push(""); } else { htmls.push(""); } } htmls.push(""); } htmls.push("
" + myMonth[0][0] + "" + myMonth[0][1] + "" + myMonth[0][2] + "" + myMonth[0][3] + "" + myMonth[0][4] + "" + myMonth[0][5] + "" + myMonth[0][6] + "
" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "
"); htmls.push("

"); htmls.push("

"); return htmls.join(''); } };

页面效果代码:

  
  
  
    

每日签到

签到

js调用方法

var str = calUtil.drawCal(current.getFullYear(),current.getMonth() + 1,signList);  
$("#calendar").html(str);

说明:signList是后台查询的已签到的时间集合,传入到js方法中会去判断哪一天签到了,然后改变签到天的显示效果,如上图!
此签到页面同样适应手机浏览器哟!

提要:
本文以写当前时间环境下当月的日历表为例,用最简单的方法实现JavaScript日历,旨在展示JS世界中实用为本、简单为上的解决问题的思路。

Web页中的日历一般离不开表格,通常都使用表格装载指定月的日期等信息。所以,要编写JS日历,首先必须解决的问题是表格的行与列问题。列是固定的,七列,因为一周有七天。行需要动态计算,因为,每一个月的第一天是星期几是一个变数,因而第一天在表格中的第几个单元也就跟着变化,同时,每个月的总天数不一致也影响着各个月对表格行数的需要量。

一. 表格的行数问题

1.首先取得处理月的总天数

JS不提供此参数,我们需要计算。考虑到闰年问题会影响二月份的天数,我们先编写一个判断闰年的自编函数:

function is_leap(year) {
   return (year%100==0?res=(year%400==0?1:0):res=(year%4==0?1:0));
}

接着定义一个包含十二个月在内的月份总天数的数组:

m_days=new Array(31,28+is_leap(ynow),31,30,31,31,30,31,30,31,30,31);

m_days这个数组里,二月份的天数已经加入闰年的信息:28+is_leap(ynow)。数组元素从0开始,正好对应于JS提供的Date函数提供的getMonth返回值,即0表示一月,1表示二月,2表示三月,依此类推。

这样,各月总数可以这样取得:m_days[x]。其中,x为0至11的自然数。

2.计算处理月第一天是星期几

可以使用Date函数的getDay取得,返回的值从0到6,0表示星期一,1表示星期二,2表示星期三,其余依此类推。代码如下(假设要处理的时间为2008年3月):

    n1str=new Date(2008,3,1);
    firstday=n1str.getDay();

有了月总天数和该月第一天是星期几这两个已知条件,就可以解决表格所需行数问题:(当前月天数+第一天是星期几的数值)除以七。表格函数需要整数,因此,我们使用Math.ceil来处理:

tr_str=Math.ceil((m_days[mnow] + firstday)/7);

表格中的tr标签实际上代表表格的行,因此变量tr_str是我们往下写表格的重要依据。

二. 打印日历表格

可以使用两个for语句嵌套起来实现:外层for语句写行,内层for语句写单元格。

for(i=0;ic0e3a42f34bb51b8aa8150d86e6d6f24");   for(k=0;k1d83b01516e611deefc2760bcec56605");
} //外层for语句结束

单元格的自然序号是否代表有效日期非常关键,为此必须加入一个过滤机制:仅打印有效的日期。有效的日期大于0小于小于等于处理月的总天数。

三. 以下是完整的JS日历代码:

function is_leap(year) { 
   return (year%100==0?res=(year%400==0?1:0):res=(year%4==0?1:0));
} //是否为闰年var nstr=new Date(); //当前Date资讯var ynow=nstr.getFullYear(); //年份var mnow=nstr.getMonth(); //月份var dnow=nstr.getDate(); //今日日期var n1str=new Date(ynow,mnow,1); //当月第一天Date资讯var firstday=n1str.getDay(); //当月第一天星期几var m_days=new Array(31,28+is_leap(ynow),31,30,31,30,31,31,30,31,30,31); //各月份的总天数var tr_str=Math.ceil((m_days[mnow] + firstday)/7); //表格所需要行数//打印表格第一行(有星期标志)document.write ("068a9e908c842749704f5310808b81bda34de1251f0d9fe1e645927f19a896e86f4c49da5a9f6db9c6afb6aaf29dac57日b90dd5946f0946207856a8a37f441edf6f4c49da5a9f6db9c6afb6aaf29dac57一b90dd5946f0946207856a8a37f441edf6f4c49da5a9f6db9c6afb6aaf29dac57二b90dd5946f0946207856a8a37f441edf6f4c49da5a9f6db9c6afb6aaf29dac57三b90dd5946f0946207856a8a37f441edf6f4c49da5a9f6db9c6afb6aaf29dac57四b90dd5946f0946207856a8a37f441edf6f4c49da5a9f6db9c6afb6aaf29dac57五b90dd5946f0946207856a8a37f441edf6f4c49da5a9f6db9c6afb6aaf29dac57六b90dd5946f0946207856a8a37f441edffd273fcf5bcad3dfdad3c41bd81ad3e5");for(i=0;i

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。更多相关教程请访问JavaScript视频教程

相关推荐:

php公益培训视频教程

JavaScript图文教程

JavaScript在线手册

以上是JavaScript实现每日签到功能的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:csdn.net。如有侵权,请联系admin@php.cn删除