职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 992|回复: 3

javascript 日期 加减

[复制链接]
dgzxg 发表于 2009-8-27 09:39 | 显示全部楼层 |阅读模式
javascript给一个日期增加45天,,,我搜索了半天怎么全都是不正确的代码呢,有做dateadd的,可是不正确,,,不准确 还能出2月30日 谁有准确的代码啊,,求啊!!!
诗诺 发表于 2009-8-27 09:39 | 显示全部楼层

javascript 日期 加减

var dat=new Date();
dat.setDate(dat.getDate()+45);
容总 发表于 2009-8-27 09:39 | 显示全部楼层

javascript 日期 加减

//******************************************************//
////
//日期对象类库//
////
//******************************************************//
/*
* 类名: DateTime
* 说明: 兼容ASP.NET日期类型
* 接口:
* addDays(n) - 增加n天
*/
// Example:
// var d = new DateTime();
// var d = new DateTime(dateObject);
// var d = new DateTime(dateString);
// var d = new DateTime(year, month, date, hours, minutes, seconds,ms)
var CST_DATE_SPLIT = '-';
var CST_DATE_TIME_SPLIT = ' ';
var CST_TIME_SPLIT = ':';
/*
* 对象: DateTime
* 说明: 构造
* 参数:
* year - 日期实例
* year - 日期字符串
* year,month,day,hour,minute,second,ms - 年,月,日,时,分,秒,毫秒
*/
function DateTime(year, month, day, hour, minute, second, ms){
var d = new Date();

// 属性定义
this.Year = d.getFullYear();
this.Month = d.getMonth();
this.Day = d.getDay();
this.Hour = this.Minute = this.Second = this.Millisecond = 0;

// 方法定义
this.toString = function(){
return digi(this.Year,4) + CST_DATE_SPLIT +
digi(this.Month, 2) + CST_DATE_SPLIT +
digi(this.Day, 2) + CST_DATE_TIME_SPLIT +
digi(this.Hour,2) + CST_TIME_SPLIT +
digi(this.Minute,2) + CST_TIME_SPLIT +
digi(this.Second,2) + CST_DATE_TIME_SPLIT +
digi(this.Millisecond, 3);
}
this.toDateString = function(splitter){
var s = '-';
if (splitter != null) s = splitter;
return digi(this.Year,4) + ((s=='cn') ? '\u5E74' : s) +
digi(this.Month, 2) + ((s=='cn') ? '\u6708' : s) +
digi(this.Day, 2) + ((s=='cn') ? '\u65E5' : '');
}
this.toTimeString = function(splitter){
var s = ':';
if (splitter != null) s = splitter;
return digi(this.Hour,2) + ((s=='cn') ? '\u65F6' : s) +
digi(this.Minute,2) + ((s=='cn') ? '\u5206' : s) +
digi(this.Second,2) + ((s=='cn') ? '\u79D2' : '');
}
this.toDateTimeString = function(splitter){
var s = ':';
if (splitter != null) s = splitter;
return digi(this.Year,4) + ((s=='cn') ? '\u5E74' : s) +
digi(this.Month, 2) + ((s=='cn') ? '\u6708' : s) +
digi(this.Day, 2) + ((s=='cn') ? '\u65E5' : '') +
' ' +
digi(this.Hour,2) + ((s=='cn') ? '\u65F6' : ':') +
digi(this.Minute,2) + ((s=='cn') ? '\u5206' : ':') +
digi(this.Second,2) + ((s=='cn') ? '\u79D2' : '');
}
this.clone = function(dt){
this.Year = dt.Year;
this.Month = dt.Month;
this.Day = dt.Day;
this.Hour = dt.Hour;
this.Minute = dt.Minute;
this.Second = dt.Second;
this.Millisecond = dt.Millisecond;
}
this.parseDate = function(date){
this.Year = date.getFullYear();
this.Month = (date.getMonth() + 1);
this.Day = date.getDate();
this.Hour = date.getHours();
this.Minute = date.getMinutes();
this.Second = date.getSeconds();
this.Millisecond = date.getMilliseconds()
}
this.toDate = function(){
return new Date(this.Year, this.Month, this.Day, this.Hour, this.Minute, this.Second, this.Millisecond);
}
this.addDays = function(value){
var CST_DAY_MILLISECONDS = 86400000;
var vd = CST_DAY_MILLISECONDS * value;
var d = this.toDate();
d.setMilliseconds(d.getMilliseconds() + vd);
this.parseDate(d);
}
this.addYears
this.addMonths = function(value){
// to be do...
}
this.addMonths = function(value){
// to be do...
}
this.addHours = function(value){
this.addDays(value/24);
}
this.addMinutes = function(value){
this.addHours(value/60);
}
this.addSeconds = function(value){
this.addMinutes(value/60);
}

this.parseString = function(src){
var o = Date.parseDate(src);
if (o == null || o.getType == null || o.getType() != "Date"){
return null;
}
else{
this.parseDate(o);
}
}

// 实例化
var args = arguments;
if (args.length == 1){
var o = args[0];
if(typeof(o) == "string"){
o = Date.parseDate(o);
}
if (o == null || o.getType == null || o.getType() != "Date"){
return null;
}
else{
this.parseDate(o);
}
}
else if (args.length >= 3){
this.Year= parseInt(args[0]);
this.Month = parseInt(args[1]);
this.Day = parseInt(args[2]);
}
if (args.length >= 4){
this.Hour= parseInt(args[3]);
}
if (args.length >= 5){
this.Minute= parseInt(args[4]);
}
if (args.length >= 6){
this.Second= parseInt(args[5]);
}
if (args.length >= 7){
this.Millisecond= parseInt(args[6]);
}
}

function digi(v, c){
v = v + "";
var n = "0000";
if(v.length < c)
return n.substr(0, c-v.length) + v;
return v;
}

DateTime.prototype = new Object();
DateTime.getType = function(){
return "DateTime";
}
/*
* 函数: (静态)parseXml
* 说明: 从XML字符串解释
* 参数:
* xml - XML 描述
* 示例:
* <Object type="DateTime">2005-01-01</Object>
*/
DateTime.parseXml = function(xml, binary){
var doc = XmlDocument();
doc.loadXML(xml);
return new DateTime(doc.text);
//var xml = "<Object type=\"DateTime\">" + s1 + "</Object>";
}
会玩就好 发表于 2011-8-9 10:49 | 显示全部楼层
本人姓:常...名:有理..
您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-26 09:20 , Processed in 0.121144 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表