var d = new Date(); var new_date = new Date(d.getFullYear(), d.getMonth(), d.getDate()+30) m = new_date.getMonth()+1 m = "0" + m m = m.substr(m.length-2,2) dt = new_date.getDate() dt = "0" + dt dt = dt.substr(dt.length-2,2) s = m + "/" + dt + "/" + new_date.getFullYear()document.form_name.txt_name.value = s
拼拼,凑个数。 Date.prototype.Format = function(fmt) { //author: meizz var o = { "M+" : this.getMonth() + 1, //月份 "d+" : this.getDate(), //日 "h+" : this.getHours(), //小时 "m+" : this.getMinutes(), //分 "s+" : this.getSeconds(), //秒 "q+" : Math.floor((this.getMonth() + 3) / 3), //季度 "S" : this.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length))); return fmt; } Date.prototype.addDays = function(d) { this.setDate(this.getDate() + d); };Date.prototype.addWeeks = function(w) { this.addDays(w * 7); };Date.prototype.addMonths= function(m) { var d = this.getDate(); this.setMonth(this.getMonth() + m); if (this.getDate() < d) this.setDate(0); };Date.prototype.addYears = function(y) { var m = this.getMonth(); this.setFullYear(this.getFullYear() + y); if (m < this.getMonth()) { this.setDate(0); } };var now = new Date(); now.addDays(30); alert(now.Format("MM/dd/yyyy"));
我多数时间是在做桌面开发。这几天写javascript有点上瘾就来学习一下呵呵
var d = new Date(); var new_date = new Date(d.getFullYear(), d.getMonth(), d.getDate()+30) m = new_date.getMonth()+1 m = "0" + m m = m.substr(m.length-2,2) dt = new_date.getDate() dt = "0" + dt dt = dt.substr(dt.length-2,2) s = m + "/" + dt + "/" + new_date.getFullYear() document.getelementbyid(你要赋值的ID).value = s
清洁工的版本里写得很清楚 是meizz大哥的作品 //author: meizz另外LS各位的方法是不是扰了大圈。无非是加30天而已。Date.prototype.addDay = function(num){if(!isNaN(num))this.setDate(this.getDate() + parseInt(num));return this;}//给日期原型加个方法var date = new Date();//创建日期实例 date.addDay(30);//调用原型方法加30天 alert(date.LocaleString());
var new_date = new Date(d.getFullYear(), d.getMonth(), d.getDate()+30)
m = new_date.getMonth()+1
m = "0" + m
m = m.substr(m.length-2,2)
dt = new_date.getDate()
dt = "0" + dt
dt = dt.substr(dt.length-2,2)
s = m + "/" + dt + "/" + new_date.getFullYear()document.form_name.txt_name.value = s
Date.prototype.Format = function(fmt)
{
//author: meizz
var o =
{
"M+" : this.getMonth() + 1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth() + 3) / 3), //季度
"S" : this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt))
fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
} Date.prototype.addDays = function(d)
{
this.setDate(this.getDate() + d);
};Date.prototype.addWeeks = function(w)
{
this.addDays(w * 7);
};Date.prototype.addMonths= function(m)
{
var d = this.getDate();
this.setMonth(this.getMonth() + m); if (this.getDate() < d)
this.setDate(0);
};Date.prototype.addYears = function(y)
{
var m = this.getMonth();
this.setFullYear(this.getFullYear() + y); if (m < this.getMonth())
{
this.setDate(0);
}
};var now = new Date();
now.addDays(30);
alert(now.Format("MM/dd/yyyy"));
var new_date = new Date(d.getFullYear(), d.getMonth(), d.getDate()+30)
m = new_date.getMonth()+1
m = "0" + m
m = m.substr(m.length-2,2)
dt = new_date.getDate()
dt = "0" + dt
dt = dt.substr(dt.length-2,2)
s = m + "/" + dt + "/" + new_date.getFullYear()
document.getelementbyid(你要赋值的ID).value = s
//author: meizz另外LS各位的方法是不是扰了大圈。无非是加30天而已。Date.prototype.addDay = function(num){if(!isNaN(num))this.setDate(this.getDate() + parseInt(num));return this;}//给日期原型加个方法var date = new Date();//创建日期实例
date.addDay(30);//调用原型方法加30天
alert(date.LocaleString());
jf jf
jf