"4楼的的(代码)难道不正确吗" ------ 1。判断闰年不准确(尽管在前后九十年内是准确的) 2。代码略微有点长,不大好理解。 建议改成如下: ----------- <script> Date.getDayOfMonth = function(y,m){ //月份从零开始 return (new Date(y,m+1,0)).getDate(); }; Date.prototype.getDateOfPreMonth = function(){ var d=new Date(this.getFullYear(),this.getMonth()-1,this.getDate()); if(d.getDate() != this.getDate()) d.setDate(0); return d; }; var d = new Date('2007/03-30').getDateOfPreMonth(); alert(d);</script>
TO Date.prototype.getDayOfMonth = function(Mm){ var Feb = (this.getFullYear() % 4 == 0)?29:28; var aM = new Array(31, Feb , 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); return (typeof Mm == 'undefined')?aM[this.getMonth()-1]:aM[Mm-1]; }; Date.prototype.getDateOfPreMonth = function(){
var dd=new Date(d.getFullYear(),d.getMonth() -1,d.getDate() );
Date.prototype.getDayOfMonth = function(Mm){
var Feb = (this.getFullYear() % 4 == 0)?29:28;
var aM = new Array(31, Feb , 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
return (typeof Mm == 'undefined')?aM[this.getMonth()-1]:aM[Mm-1];
};
Date.prototype.getDateOfPreMonth = function(){
return new Date(
(this.getMonth() == 0)?(this.getFullYear()-1):this.getFullYear() ,
(this.getMonth() == 0)?11:this.getMonth() ,
this.getDayOfMonth(
(this.getMonth() == 0)?11:(this.getMonth()-1)
<=
this.getDate()
)?this.getDate():
this.getDayOfMonth(
(this.getMonth() == 0)?11:(this.getMonth()-1)
)
);
};
var d = new Date().getDateOfPreMonth();
alert(d);
var d1=new Date('2007/01/10');
//var d1=new Date('2007/03/30'); var d2=new Date(d1.getFullYear(),d1.getMonth()-1,d1.getDate());
if(d2.getDate()!=d1.getDate()) d2.setDate(0);
alert(d2);
</script>
lz你的问题有一个小小的问题:3月31日的上一个月的年月日应该是多少?
alert(currentDate.getFullYear()+'-'+ (currentDate.getMonth()+1) +'-'+currentDate.getDate()); //当前年月日var previewDate=new Date(currentDate.getFullYear(),currentDate.getMonth()-1,currentDate.getDate());
if(previewDate.getDate()!=currentDate.getDate())
{previewDate.setDate(0);}
alert(previewDate);
按照上面两位红星同志的做法,结果弹出上月的年月日格式都为"Sun Nov 18 00:00:00 UTC+0800 2007"如何改进?
JScript setMonth 方法
请参阅
Date 对象的方法 | getMonth 方法 | getUTCMonth 方法 | setUTCMonth 方法应用于:Date 对象
要求
版本 1
设置 Date 对象中用本地时间表示的月份值。dateObj.setMonth(numMonth[, dateVal])
参数
dateObj
必选项。任意 Date 对象。
numMonth
必选项。一个等于月份值的数值。
dateVal
可选项。一个代表日期的数值。如果没有提供此参数,那么将使用通过调用 getDate 方法而得到的数值。
说明
要设置用全球标准时间 (UTC) 表示的月份值,请使用 setUTCMonth 方法。如果 numMonth 的值大于 11 (月份 0 表示一月)或者是一个负数,那么所保存的年份将相应地得到改变。例如,如果所保存的日期是 "Jan 5, 1996" 并且调用了 setMonth(14) 方法,那么该日期就被改变为 "Mar 5, 1997."示例
下面这个例子说明了 setMonth 方法的用法。function SetMonthDemo(newmonth){
var d, s; // 声明变量。
d = new Date(); // 创建 Date 对象
d.setMonth(newmonth); // 设置月份。
s = "Current setting is ";
s += d.toLocaleString();
return(s); //返回新的设置。
}
难道不正确吗?]按照上面两位红星同志的做法,结果弹出上月的年月日格式都为"Sun Nov 18 00:00:00 UTC+0800 2007" 如何改进? 也测试过你的,同样的格式问题
所以alert(date)默认就调用了toString方法...
所以..
你只需要用d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
即可
难道不正确吗?] 按照上面两位红星同志的做法,结果弹出上月的年月日格式都为"Sun Nov 18 00:00:00 UTC+0800 2007" 如何改进? 也测试过你的,同样的格式问题
----------因为是Date型的 ,肯定是这样了呀你可以 d.getFullYear()+ "-"+ d.getMonth() +"-"+ d.getDate()就可以了当然如 muxrwc 所说 ,直接调系统内置方法将更好 ,更可取
------
1。判断闰年不准确(尽管在前后九十年内是准确的)
2。代码略微有点长,不大好理解。
建议改成如下:
-----------
<script>
Date.getDayOfMonth = function(y,m){ //月份从零开始
return (new Date(y,m+1,0)).getDate();
};
Date.prototype.getDateOfPreMonth = function(){
var d=new Date(this.getFullYear(),this.getMonth()-1,this.getDate());
if(d.getDate() != this.getDate()) d.setDate(0);
return d;
};
var d = new Date('2007/03-30').getDateOfPreMonth();
alert(d);</script>
var Feb = (this.getFullYear() % 4 == 0)?29:28;
var aM = new Array(31, Feb , 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
return (typeof Mm == 'undefined')?aM[this.getMonth()-1]:aM[Mm-1];
};
Date.prototype.getDateOfPreMonth = function(){
return new Date(
(this.getMonth() == 0)?(this.getFullYear()-1):this.getFullYear() ,
(this.getMonth() == 0)?11:this.getMonth() ,
this.getDayOfMonth(
(this.getMonth() == 0)?11:(this.getMonth()-1)
<=
this.getDate()
)?this.getDate():
this.getDayOfMonth(
(this.getMonth() == 0)?11:(this.getMonth()-1)
)
);
};
var previewDate = new Date().getDateOfPreMonth();
//alert(d);
alert(PreviewDate.getFullYear()+'-'+ (PreviewDate.getMonth()) +'-'+PreviewDate.getDate());当当前年月日为"2007-01-17"时,alert的值为"2006-11-17",相差多了一个月.其它都正确.