javascript datetime timespan
来源:广州中睿信息技术有限公司官网
发布时间:2012/10/21 23:25:16 编辑:admin 阅读 443
/*similarto.NET'sDateTimeclass--slchen*/functionDateTime(){this.year=0this.month=0this.day=0this.ho
/*similar to .NET's DateTime class--slchen
*/
function DateTime() {
    
this.year = 0;
    
this.month = 0;
    
this.day = 0;
    
this.hour = 0;
    
this.minute = 0;
    
this.second = 0;
    
this.millisecond = 0;

    
switch (arguments.length) {
        
case 0:
            
var d = new Date();
            
this.year = d.getFullYear();
            
this.month = d.getMonth() + 1;
            
this.day = d.getDate();
            
this.hour = d.getHours();
            
this.minute = d.getMinutes();
            
this.second = d.getSeconds();
            
this.millisecond = d.getMilliseconds();
            
break;
        
case 1:
            
this.millisecond = arguments[0];
            
break;
        
case 3:
            
this.year = arguments[0];
            
this.month = arguments[1];
            
this.day = arguments[2];
            
break;
        
case 6:
            
this.year = arguments[0];
            
this.month = arguments[1];
            
this.day = arguments[2];
            
this.hour = arguments[3];
            
this.minute = arguments[4];
            
this.second = arguments[5];
            
break;
        
case 7:
            
this.year = arguments[0];
            
this.month = arguments[1];
            
this.day = arguments[2];
            
this.hour = arguments[3];
            
this.minute = arguments[4];
            
this.second = arguments[5];
            
this.millisecond = arguments[6];
            
break;
        
default:
            
throw ("No constructor");
    }

    
this.strings = function () { };
    
this.strings.TimeSeparator = ":";
    
this.strings.DateSeparator = "-";
    
this.strings.SpaceSeparator = " ";

    
this.toString = function () {
        
return this.getDate() + this.strings.SpaceSeparator + this.getTime();
    }


    
this.getDate = function () {
        
return this.year + this.strings.DateSeparator + this.Pad(this.month) + this.strings.DateSeparator + this.Pad(this.day);
    }


    
this.getTime = function () {
        
return this.strings.SpaceSeparator + this.Pad(this.hour) + this.strings.TimeSeparator + this.Pad(this.minute) + this.strings.TimeSeparator + this.Pad(this.second);
    }

    
this.getYear = function () {
        
return this.year;
    }

    
this.getMonth = function () {
        
return this.month;
    }

    
this.getDay = function () {
        
return this.day;
    }

    
this.getHour = function () {
        
return this.hour;
    }

    
this.getMinute = function () {
        
return this.minute;
    }

    
this.getSecond = function () {
        
return this.second;
    }

    
this.span = new Date(this.year, this.month - 1this.day, this.hour, this.minute, this.second, this.millisecond);

    
this.getTick = function () {
        
return new Date(this.year, this.month - 1this.day, this.hour, this.minute, this.second, this.millisecond).getTime();
    }

    
this.CompareTo = function (datetime1) {
        
if (this.getTick() > datetime1.getTick()) return 1;
        
if (this.getTick() == datetime1.getTick()) return 0;
        
if (this.getTick() < datetime1.getTick()) return -1;
    }

    
this.addYears = function (years) {
        
this.span.setYear(this.year + years);
        
var year = this.span.getFullYear();
        
var month = this.span.getMonth() + 1;
        
var day = this.span.getDate();
        
var hour = this.span.getHours();
        
var minute = this.span.getMinutes();
        
var second = this.span.getSeconds();
        
var millisecond = this.span.getMilliseconds();
        
return new DateTime(year, month, day, hour, minute, second, millisecond);
    }

    
this.addMonths = function (months) {
        
this.span.setMonth(this.month - 1 + months);
        
var year = this.span.getFullYear();
        
var month = this.span.getMonth() + 1;
        
var day = this.span.getDate();
        
var hour = this.span.getHours();
        
var minute = this.span.getMinutes();
        
var second = this.span.getSeconds();
&n
联系我们CONTACT 扫一扫
愿景:成为最专业的软件研发服务领航者
中睿信息技术有限公司 广州•深圳 Tel:020-38931912 务实 Pragmatic
广州:广州市天河区翰景路1号金星大厦18层中睿信息 Fax:020-38931912 专业 Professional
深圳:深圳市福田区车公庙有色金属大厦509~510 Tel:0755-25855012 诚信 Integrity
所有权声明:PMI, PMP, Project Management Professional, PMI-ACP, PMI-PBA和PMBOK是项目管理协会(Project Management Institute, Inc.)的注册标志。
版权所有:广州中睿信息技术有限公司 粤ICP备13082838号-2