javascript中Date对象的应用——简易日历的实现
前面的话
简易日历作为javascript中Date对象的常见应用,用途较广泛。本文将详细说明简易日历的实现思路
效果演示
HTML说明
使用type=number的两个input分别作为年和月的输入控件,这样在高级浏览器下自带调节按钮
按照周日到周一的顺序进行星期的排列
<div class="box">
<header class='control'>
<input id="conYear" class="con-in" type="number" min="1900" max="2100" step="1"/>
<input id="conMonth" class="con-in" type="number" min="1" max="12" step="1"/>
</header>
<div class="DateBox">
<header class='week'>
<div class="week-in">周日</div>
<div class="week-in">周一</div>
<div class="week-in">周二</div>
<div class="week-in">周三</div>
<div class="week-in">周四</div>
<div class="week-in">周五</div>
<div class="week-in">周六</div>
</header>
<section class="dayBox" id='dayBox'>
<div class="day" id="day1">1</div>
<div class="day">2</div>
<div class="day">3</div>
<div class="day">4</div>
<div class="day">5</div>
<div class="day">6</div>
<div class="day">7</div>
<div class="day">8</div>
<div class="day">9</div>
<div class="day">10</div>
<div class="day">11</div>
<div class="day">12</div>
<div class="day">13</div>
<div class="day">14</div>
<div class="day">15</div>
<div class="day">16</div>
<div class="day">17</div>
<div class="day">18</div>
<div class="day">19</div>
<div class="day">20</div>
<div class="day">21</div>
<div class="day">22</div>
<div class="day">23</div>
<div class="day">24</div>
<div class="day">25</div>
<div class="day">26</div>
<div class="day">27</div>
<div class="day">28</div>
<div class="day">29</div>
<div class="day" id="day30">30</div>
<div class="day" id="day31">31</div>
</section>
</div>
</div>
CSS说明
对于简易日历的实现,首先确定日历中class="day"的div的排列方式为浮动。这样可以通过改变第一天div的位置,来实现所有同级div都可以跟随移动的效果
body{
margin:;
}
input{
border: none;
padding:;
}
.box{
width: 354px;
margin: 30px auto 0;
}
.DateBox{
height: 300px;
border: 2px solid black;
}
.week{
overflow: hidden;
border-bottom: 1px solid black;
line-height: 49px;
}
.week-in{
height: 49px;
float: left;
width: 50px;
text-align: center;
}
.dayBox{
overflow: hidden;
}
.day{
float: left;
height: 50px;
width: 50px;
font:20px/50px '微软雅黑';
text-align: center;
}
.control{
overflow: hidden;
}
.con-in{
height: 50px;
float: left;
width: 100px;
text-align: center;
font: 20px/50px "微软雅黑";
}
JS说明
简易日历的JS逻辑总共需要5个实现:
【1】需要获取当月的天数,获取当月第一天、第30天、第31天是周几
【2】根据当月第一天的星期,改变第一天的margin-left值,移动第一天到对应的位置;由于浮动的关系,其余天也会跟着移动到对应的位置
【3】根据当月的天数,隐藏多余的天;当然,隐藏之前要先显示在其他月份可能被隐藏的天
【4】如果当月30日是周日,则会新占一行。这时通过改变30日这天的margin值将其移动到第一行(若31日可能会新占一行,也做相似处理)
【5】载入页面后,获取当前的年和月,显示当月日历;当改变年或月时,获取改变后的值,更新日历
//准备:获取当前样式
function getCSS(obj,style){
if(window.getComputedStyle){
return getComputedStyle(obj)[style];
}
return obj.currentStyle[style];
}
//实现一:获取当月的天数,及当月第一天、第30日、第31日是星期几
function get_data(year,month){
var result = {};
var d = new Date();
//如果是2月
if(month == 2){
//如果是闰年
if((year % 4 === 0 && year % 100 !== 0) || year % 400 === 0){
result.days = 29;
//如果是平年
}else{
result.days = 28;
}
//如果是第4、6、9、11月
}else if(month == 4 || month == 6 ||month == 9 ||month == 11){
result.days = 30;
}else{
result.days = 31;
//当月第31天是星期几
result.day31week = d.getDay(d.setFullYear(year,month-1,31));
}
//当月第一天是星期几
result.day1week = d.getDay(d.setFullYear(year,month-1,1));
if(month != 2){
//当月第30天是星期几
result.day30week = d.getDay(d.setFullYear(year,month-1,30));
}
return result;
}
//实现二:根据当月第一天的星期x,设置第一天的margin-left=宽度*x,使其对应到正确的星期位置上
function move_day1(year,month){
var week1 = get_data(year,month).day1week;
day1.style.marginLeft = week1%7*parseInt(getCSS(day1,'width'))+ 'px';
}
//实现三:根据当月的天数,来隐藏多余的天数。当然首先要先显示在其他月份被隐藏的天数
function hide_days(year,month){
//恢复其他月份可能隐藏的天数
for(var i = 28; i<31; i++){
dayBox.children[i].style.display = 'block';
}
//隐藏当月多余的天数
var days = get_data(year,month).days;
for(var i = days;i<31;i++){
dayBox.children[i].style.display = 'none';
}
};
//实现四:如果当月30日或31日是星期日,则会新占一行,通过设置margin-top把新占一行的天移动到第一行
function move_day30(year,month){
//如果当月30日是星期日
if(get_data(year,month).day30week === 0){
day30.style.marginTop = parseInt(getCSS(day30,'height')) *(-5) + 'px';
day31.style.marginTop = parseInt(getCSS(day31,'height')) *(-5) + 'px';
day31.style.marginLeft= getCSS(day31,'width');
return;
}else{
day30.style.marginTop = day31.style.marginTop = day31.style.marginLeft ='0';
}
//如果当月31日是星期日
if(get_data(year,month).day31week === 0){
day31.style.marginTop = parseInt(getCSS(day31,'height')) *(-5) + 'px';
}else{
day31.style.marginTop = '0';
}
}
//实现五:当载入页面时,获取当前年和月,显示当月日历;当改变年或月时,获取改变后的年和月,更新当月日历
var year= conYear.value=new Date().getFullYear();
var month= conMonth.value = new Date().getMonth() + 1;
move_day1(year,month);
hide_days(year,month);
move_day30(year,month); conYear.onchange = conMonth.onchange = function(){
var year = conYear.value;
var month = conMonth.value;
if(year<1900 || year >2100 ){
year = conYear.value=new Date().getFullYear();
}
if(month<1 || month > 12){
month = conMonth.value=new Date().getMonth() + 1;
}
move_day1(year,month);
hide_days(year,month);
move_day30(year,month);
}
javascript中Date对象的应用——简易日历的实现的更多相关文章
- javascript中Date对象的应用
前面的话 简易日历作为javascript中Date对象的常见应用,用途较广泛.本文将详细说明简易日历的实现思路 效果演示 HTML说明 使用type=number的两个input分别作为年和月的输入 ...
- JavaScript中date 对象常用方法
Date 对象 Date 对象用于处理日期和时间. //创建 Date 对象的语法: var datetime = new Date();//Date 对象会自动把当前日期和时间保存为其初始值. co ...
- Javascript中Date对象的格式化
很多语言中都带有日期的格式化函数,而Javascript中却没有提供类似的方法.之前从某位前人的帖子中发现了下面的代码,感觉非常简洁,存留备用. /** * 时间对象的格式化; */ Date.pro ...
- javascript中Date对象复习
js的Date对象不怎么经常用,所以忘得差不多,复习一下 1.声明一个Date对象,默认本地当前时间 var date = new Date();//Fri Apr 28 2017 14:26:19 ...
- JavaScript中判断对象类型方法大全1
我们知道,JavaScript中检测对象类型的运算符有:typeof.instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一 ...
- JavaScript中判断对象类型的种种方法
我们知道,JavaScript中检测对象类型的运算符有:typeof.instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一 ...
- 转 JavaScript中判断对象类型的种种方法
我们知道,JavaScript中检测对象类型的运算符有:typeof.instanceof,还有对象的constructor属性: 1) typeof 运算符 typeof 是一元运算符,返回结果是一 ...
- js实现小时钟,js中Date对象的使用?
介绍一下js中Date对象的使用 dateObj = new Date() dateObj = new Date(dateValue) dateObj = new Date(year,month,da ...
- JavaScript中的对象与原型—你不知道的JavaScript上卷读书笔记(四)
一.对象 对象可以通过两种形式定义:声明(文字)形式和构造形式.即: var myObj = { key: value // ... }; 或: var myObj = new Object(); m ...
随机推荐
- WPF控件经验小结:(1) ToolBar去掉右边箭头(扩展图标)
今天开发时,同事问我一个问题.怎么去除ToolBar右边扩展图标.我想了一下,说改Style.同事说太麻烦了.可不可以快速修正.我说应该动态去读取Template模板中的Style,然后隐藏.怎么实现 ...
- Amazon的推荐系统
本文引自http://blog.csdn.net/fwing/article/details/4942886 现在的推荐系统特别火啊.做得最好的应该是Amazon了. 上面是Amazon的图书推荐. ...
- Java中的一个类怎么调用另一个类中的方法
如果另一个类中的那个方法是私有的话,就不能直接调用到,如果是其他类型的话看情况,如果是静态的(static)话,直接用类名可以调用到,如果是非静态的,就需要利用另一个类的实例(也就是用那个类生成的对象 ...
- 读入一个c程序,并按字母表顺序分组打印变量名,每组前N个字符相同(TCPL 练习6-2)
在建立结构tnode的过程中,我们没有预设门槛.这道题目就设置了门槛,必须根据前N个字符来进行分组,于是排除了长度小于N的变量,以便减轻负担. 因为要求对变量名分组打印,组别理所应当地应该按照至少是升 ...
- Spring 使用JSTL标签显示后台数据
1. 先上项目结构图,其中config包下的代码文件参见前一篇博客 http://www.cnblogs.com/njust-ycc/p/6123505.html 引包: 2. 主要代码 (1)U ...
- 【leetcode】Pow(x,n)
马上各种校招要开始了,怎么也得准备一下,之前一直在看看机器学习,NLP方面的东西,收获很多.最近换换脑子,回过头来做做leetcode,感觉还是蛮有意思的.今天刷了个水题,AC不高,然而难度也不高.. ...
- 【转】C#Winform程序如何发布并自动升级(图解)
有不少朋友问到C#Winform程序怎么样配置升级,怎么样打包,怎么样发布的,在这里我解释一下打包和发布关于打包的大家可以看我的文章C# winform程序怎么打包成安装项目(图解)其实打包是打包,发 ...
- Ember.js之动态创建模型
本人原文地址发布在:点击这里 What problem did we meet? As ember document suggestion, we may define a model as a st ...
- Unity3D逻辑热更新,第二代舒爽解决方案,L#使用简介
热更新 天下武功,无坚不破,唯快不破 热更新就是为了更快的把内容推到用户手中. 之前,我设计了C#Light,经过半年多的持续修补,勉强可用,磕磕绊绊.感谢那些,试过,骂过,用过的朋友,在你们的陪伴下 ...
- Android移动APP开发笔记——Cordova(PhoneGap)通过CordovaPlugin插件调用 Activity 实例
引言 Cordova(PhoneGap)采用的是HTML5+JavaScript混合模式来开发移动手机APP,因此当页面需要获取手机内部某些信息时(例如:联系人信息,坐标定位,短信等),程序就需要调用 ...