JS Date

jsjqjy發表於2009-12-21
<script language=javascript>
//一天含 86,400,000 毫秒(24* 60 * 60*1000)

//今天
function showToDay()
{
var Nowdate=new Date();
M=Number(Nowdate.getMonth())+1
return Nowdate.getYear()+"-"+M+"-"+Nowdate.getDate();
}

//本週第一天
function showWeekFirstDay()
{
var Nowdate=new Date();
var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
return WeekFirstDay;
}
//本週最後一天
function showWeekLastDay()
{
var Nowdate=new Date();
var WeekFirstDay=new Date(Nowdate-(Nowdate.getDay()-1)*86400000);
var WeekLastDay=new Date((WeekFirstDay/1000+6*86400)*1000);
return WeekLastDay;
}

//本月第一天
function showMonthFirstDay()
{
var Nowdate=new Date();
var MonthFirstDay=new Date(Nowdate.getYear(),Nowdate.getMonth(),1);
return MonthFirstDay;
}
//本月最後一天
function showMonthLastDay()
{
var Nowdate=new Date();
var MonthNextFirstDay=new Date(Nowdate.getYear(),Nowdate.getMonth

()+1,1);
var MonthLastDay=new Date(MonthNextFirstDay-86400000);
return MonthLastDay;
}
//上月第一天
function showPreviousFirstDay()
{
var MonthFirstDay=showMonthFirstDay()
return new Date(MonthFirstDay.getYear(),MonthFirstDay.getMonth()-

1,1)
}
//上月最後一天
function showPreviousLastDay()
{
var MonthFirstDay=showMonthFirstDay();
return new Date(MonthFirstDay-86400000);
}
//上週第一天
function showPreviousFirstWeekDay()
{
var WeekFirstDay=showWeekFirstDay()
return new Date(WeekFirstDay-86400000*7)
}
//上週最後一天
function showPreviousLastWeekDay()
{
var WeekFirstDay=showWeekFirstDay()
return new Date(WeekFirstDay-86400000)
}
//上一天
function showPreviousDay()
{
var MonthFirstDay=new Date();
return new Date(MonthFirstDay-86400000);
}
//下一天
function showNextDay()
{
var MonthFirstDay=new Date();
return new Date((MonthFirstDay/1000+86400)*1000);
}
//下週第一天
function showNextFirstWeekDay()
{
var MonthFirstDay=showWeekLastDay()
return new Date((MonthFirstDay/1000+86400)*1000)
}
//下週最後一天
function showNextLastWeekDay()
{
var MonthFirstDay=showWeekLastDay()
return new Date((MonthFirstDay/1000+7*86400)*1000)
}
//下月第一天
function showNextFirstDay()
{
var MonthFirstDay=showMonthFirstDay()
return new Date(MonthFirstDay.getYear(),MonthFirstDay.getMonth()

+1,1)
}
//下月最後一天
function showNextLastDay()
{
var MonthFirstDay=showMonthFirstDay()
return new Date(new Date(MonthFirstDay.getYear

(),MonthFirstDay.getMonth()+2,1)-86400000)
}
function Date.prototype.toString(){
return this.getFullYear()+"-"+(this.getMonth()+1)+"-"+this.getDate

();
}
function setDate(num){
if(num==1){
document.all.dateDate.value=showMonthFirstDay();
document.all.dateDate2.value=showMonthLastDay();
}
if(num==2){
document.all.dateDate.value=showWeekFirstDay();
document.all.dateDate2.value=showWeekLastDay();
}
if(num==3){
document.all.dateDate.value=showToDay();
document.all.dateDate2.value=showToDay();
}
if(num==4){
document.all.dateDate.value=showPreviousFirstDay();
document.all.dateDate2.value=showPreviousLastDay();
}
if(num==5){
document.all.dateDate.value=showNextFirstDay();
document.all.dateDate2.value=showNextLastDay();
}
if(num==6){
document.all.dateDate.value=showPreviousFirstWeekDay();
document.all.dateDate2.value=showPreviousLastWeekDay();
}
if(num==7){
document.all.dateDate.value=showNextFirstWeekDay();
document.all.dateDate2.value=showNextLastWeekDay();
}
if(num==8){
document.all.dateDate.value=showPreviousDay();
document.all.dateDate2.value=showPreviousDay();
}
if(num==9){
document.all.dateDate.value=showNextDay();
document.all.dateDate2.value=showNextDay();
}
}

</script>
<input name=dateDate type=text>-><input name=dateDate2 type=text>
&nbsp;<input name=haha type=button value="上一月" onclick=setDate

(4)>&nbsp;
&nbsp;<input name=haha type=button value="本月" onclick=setDate(1)

>&nbsp;
<input name=haha type=button value="下一月" onclick=setDate(5)

>&nbsp;
<input name=haha type=button value="上一週" onclick=setDate(6)

>&nbsp;
<input name=haha type=button value="本週" onclick=setDate(2)>&nbsp;
<input name=haha type=button value="下一週" onclick=setDate(7)

>&nbsp;
<input name=haha type=button value="上一天" onclick=setDate(8)

>&nbsp;
<input name=haha type=button value="今天" onclick=setDate(3)>&nbsp;
<input name=haha type=button value="下一天" onclick=setDate(9)>


 

 

相關文章