You are here: 首頁 Javascript相關訊息 JavaScript的取日期的問題

飛朵啦學習手札

本網站建議使用Firefox2.0以上,或是使用Goole瀏覽器來瀏覽,並使用1024x768解析度來觀看.

JavaScript的取日期的問題

E-mail 列印 PDF
PS:getYear()有Y2K問題所以建議全部轉用getFullYear();

 

PS:DATE對象GETYEAR()方法和GETFULLYEAR()方法區別
getYear()
使用getYear()函數的本意是獲取年份,以2010年為例,如:
Js代碼
var nowd = new Date();
var yf = nowd.getYear();
在IE中是可以正確獲取年份:2010,但是在FF等瀏覽器下則為:110。
原因則是在FF等瀏覽器內getYear 返回的是"當前年份-1900" 的值(即年份基數是1900)
而IE則是當today的年份大於等於2000的時,直接將1900加上了,返回的2010。
getFullYear()
使用getFullYear()在IE和FF中都可以正確獲取年份:2010
注意
使用js來獲取年份都使用:getFullYear()

 

<script   language=javascript>
//算出今天和上一個月
function LastMonth(){
var nowdate = new Date();
var lastdate = new Date();
nowdate.setMonth(nowdate.getMonth()+1);//算當月要加1
lastdate.setMonth(nowdate.getMonth()-1);
nowdate = nowdate.getFullYear()+"-"+nowdate.getMonth()+"-"+nowdate.getDate();
lastdate = lastdate.getFullYear()+"-"+lastdate.getMonth()+"-"+lastdate.getDate();
date_html = lastdate + '  ~  ' + nowdate ;
}
//----------------以下方式為舊的方式參考就好---------------------------------------------
//今天
function   showToDay()
{
var   Nowdate=new   Date();
M=Number(Nowdate.getMonth())+1
return   Nowdate.getFullYear()+"-"+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.getFullYear(),Nowdate.getMonth(),1);
return   MonthFirstDay;
}
//本月最後一天
function   showMonthLastDay()
{
var   Nowdate=new   Date();
var   MonthNextFirstDay=new   Date(Nowdate.getFullYear(),Nowdate.getMonth()+1,1);
var   MonthLastDay=new   Date(MonthNextFirstDay-86400000);
return   MonthLastDay;
}
//上月第一天
function   showPreviousFirstDay()
{
var   MonthFirstDay=showMonthFirstDay()
return   new   Date(MonthFirstDay.getFullYear(),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.getFullYear(),MonthFirstDay.getMonth()+1,1)
}
//下月最後一天
function   showNextLastDay()
{
var   MonthFirstDay=showMonthFirstDay()
return   new   Date(new   Date(MonthFirstDay.getFullYear(),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>
<input   name=haha   type=button   value="上一月"   onclick=setDate(4)>
<input   name=haha   type=button   value="本月"   onclick=setDate(1)>
<input   name=haha   type=button   value="下一月"   onclick=setDate(5)>
<input   name=haha   type=button   value="上一周"   onclick=setDate(6)>
<input   name=haha   type=button   value="本周"   onclick=setDate(2)>
<input   name=haha   type=button   value="下一周"   onclick=setDate(7)>
<input   name=haha   type=button   value="上一天"   onclick=setDate(8)>
<input   name=haha   type=button   value="今天"   onclick=setDate(3)>
<input   name=haha   type=button   value="下一天"   onclick=setDate(9)>
最近更新 ( 週二, 27 三月 2012 11:46 )  
 

新增回應


驗證碼
更新