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>";
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值