掌握Date类常用的方法[下]
10,新建图层ActionScript,加as:
//1、日历代码
//每月的天数数组
monthdays = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
//星期数组
weekstr = new Array("星期日", "星期一", "星期二",
"星期三", "星期四", "星期五", "星期六");
//每月从...开始的值变量
startday = 1;
//每周从...开始的值变量
startweek = 0;
//日历开始坐标x,y的值变量
start_x = 100;
start_y = 140;
//日期的x,y方向间隔的值变量
space_x = 48;
space_y = 32;
//最大日历行数的值变量
yrow = 0;
//保存当前显示的年、月、日的值变量
month_num = "";
year_num = "";
day_num = "";
//显示今天的日历
ShowToday();
//复制画圈的MC(circul_mc)
attachMovie("circul_mc", "circul_mc", 2000);
////////////////////////////////////////////////////////////
//下面是五个按纽的具体函数
yearprev_btn.onPress = function() {
ShowPrevYear();
};
yearnext_btn.onPress = function() {
ShowNextYear();
};
monthprev_btn.onPress = function() {
ShowPrevMonth();
};
monthnext_btn.onPress = function() {
ShowNextMonth();
};
tody_btn.onPress = function() {
ShowToday();
};
//下面是各功能函数模块
//显示当前的年、月、日、星期
function ShowDateText() {
curdate = new Date(this.yearval,
this.monthval-1, day_num);
date_txt = this.yearval+"年"
+this.monthval+"月"+day_num+"日-"+weekstr[curdate.getDay()];
delete curdate;
}
//设置年月显示格式,并显示在动态文本框
function SetMY(month_num, year_num) {
if (year_num>9999) {
year_num = 0;
} else if (year_num<0) {
year_num = 9999;
}
if (month_num>12) {
month_num = 1;
} else if (month_num<0) {
month_num = 12;
}
if (month_num<10) {
this.monthval = "0"+month_num;
this.month_txt = "0"+month_num;
} else {
this.monthval = month_num;
this.month_txt = month_num;
}
this.yearval = year_num;
}
//显示今天的日历全部
function ShowToday() {
tody = new Date();
this.yearval = tody.getFullYear();
day_num = tody.getDate();
SetMY(tody.getMonth()+1, this.yearval);
delete tody;
ShowCal();
}
//显示下月的日历全部
function ShowNextMonth() {
month_num = int(this.monthval)+1;
year_num = int(this.yearval);
if (month_num>12) {
year_num++;
month_num = 1;
}
SetMY(month_num, year_num);
ShowCal();
}
//显示上月的日历全部
function ShowPrevMonth() {
month_num = int(this.monthval)-1;
year_num = int(this.yearval);
if (month_num<1) {
year_num--;
month_num = 12;
}
SetMY(month_num, year_num);
ShowCal();
}
//显示下年的日历全部
function ShowNextYear() {
month_num = int(this.monthval);
year_num = int(this.yearval)+1;
SetMY(month_num, year_num);
ShowCal();
}
//显示上年的日历全部
function ShowPrevYear() {
month_num = int(this.monthval);
year_num = int(this.yearval)-1;
SetMY(month_num, year_num);
ShowCal();
}
//判断是否闰年
function IsLeap(year) {
bLeap = false;
if ((year%4) == 0) {
if ((year%100) == 0) {
if ((year%400) == 0) {
bLeap = true;
}
} else {
bLeap = true;
}
}
return bLeap;
}
//复制后的各个day_mc实例,完成自己的排列定位。
//这个函数有它自己的onLoad事件调用
function SetDate(clip) {
if (startday>numdays) {
clip.removeMovieClip();
return;
}
//设置第一日
clip.dateval = startday;
//第一日的位置
clip._x = start_x+startweek*space_x;
clip._y = start_y+yrow*space_y;
if (startday == day_num) {
//为了当前日画圈
circul_mc._x = clip._x;
circul_mc._y = clip._y;
}
startweek++;
startday++;
//换行
if (startweek>=7) {
startweek = 0;
yrow++;
//最多5列
if (yrow>5) {
yrow = 0;
}
}
}
//关键函数,完成整个日历的显示
function ShowCal() {
//取得每月的1日星期几
newdate = new Date(this.yearval, this.monthval-1, 1);
startweek = newdate.getDay();
startday = 1;
yrow = 0;
numdays = monthdays[this.monthval-1];
if ((this.monthval == 2) && IsLeap(this.yearval)) {
numdays = 29;
}
if (numdays< day_num) {
day_num = numdays;
}
//显示年月日星期
ShowDateText();
//复制31次day_mc实例
var i;
for (i=1; i<=31; i++) {
attachMovie("day_mc", "day_mc"+i, 1000+i);
}
delete newdate;
}
//2、时间代码
//下面的是为了显示当前时间的事件函数
onLoad = function () {
timer = new Date();
};
onEnterFrame = function () {
horas = timer.getHours();
minutos = timer.getMinutes();
segundos = timer.getSeconds();
if (horas<10) {
horas = "0"+horas;
}
if (minutos<10) {
minutos = "0"+minutos;
}
if (segundos<10) {
segundos = "0"+segundos;
}
h_txt = horas;
m_txt = minutos;
s_txt = segundos;
delete timer;
timer = new Date();
};
代码比较长,多用的是函数,大家耐心的看。关键地方我已经注释。真正的自己做一遍。掌握getFullYear()、getMonth()、getDate()、getHours()、getMinutes()、getSeconds()的用法就可以了。 如修改这个日历,可以到MC元件day中去美化具体的某日显示,可以到主场景加些背景图片,也可到day_mc 加as来改变些显示效果。