JavaScript datetime action function

Source: Internet
Author: User
Tags datetime getdate

Web page effects datetime time operation function
Html

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>datetime and TimeSpan </title>
<style>
Body {screen.css Tutorial (line 1)
Font-family:verdana,nsimsun,sans-serif;
font-size:12px;
}
p {
Background-color: #EEEEEE;
border:1px solid #CCCCCC;
padding:5px;
}
. displaycode {screen.css (line 339)
Font-family:andale Mono,lucida Console,monaco,fixed,monospace;
font-size:11px;
margin-bottom:0pt;
margin-top:0pt;
}
</style>
<script src= "Datetime.js" type= "Text/javascript" ></script>

<body>

<script language= "javascript" type= "Text/javascript" >

function Write (A, b) {
if (!a &&!b) {
document.write ("<p>---------------------------------------------</p>");
Return
}
document.write ("<p>" + A + ": <b>" + B + "</b></p>");
}

var now = new DateTime ();
Write ("Now", now);
Write ("Now Gettick", Now.gettick ());
Write ("Now getyear", Now.getyear ());
Write ("Now GetMonth", Now.getmonth ());
Write ("Now Getday", Now.getday ());
Write ("Now Gethour", Now.gethour ());
Write ("Now Getminute", Now.getminute ());
Write ("Now Getsecond", Now.getsecond ());
Write ("Now GetDate", Now.getdate ());
Write ("Now GetTime", Now.gettime ());
Write ();
var now1 = new DateTime (2011,7,23,16,12,12);
Write ("Now1", Now1);
Write ();

var now2 = now1.addminutes (100);
Write ("Now1.addhours", now2);

var t = new TimeSpan (Now2.gettick ()-Now1.gettick ());

      Write ("T.totaldays ()", t.totaldays ());
      Write ("T.totalhours ()", t.totalhours ());
      Write ("T.totalmilliseconds ()", T.totalmilliseconds ());
      Write ("T.totalminutes ()", t.totalminutes ());
      Write ("T.totalseconds ()", T.totalseconds ());
      Write ("Now.compareto (Now1)", Now.compareto (Now1));
   
     
  </script>
 
  </body
 

Js
/*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-1, This.day, This.hour, This.minute, This.second, This.millisecond);

This.gettick = function () {
return new Date (This.year, This.month-1, This.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 to 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 ();
        var millisecond = this.span.getMilliseconds ();
        return to New DateTime (year, month, day, hour, minute, second, millisecond) ;
   }


This.adddays = function (days) {
This.span.setDate (This.day + days);
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 to New DateTime (year, month, day, hour, minute, second, millisecond);

}

    this.addhours = function (hours) {
        This.span.setHours (this.hour + hours);
        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 to New DateTime (year, month, day, hour, minute, second, millisecond) ;
   }

    this.addminutes = function (minutes) {
        This.span.setMinutes (This.minute + minutes);
        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 to New DateTime (year, month, day, hour, minute, second, millisecond) ;
   }


This.addseconds = function (seconds) {
This.span.setSeconds (this.second + seconds);
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 to New DateTime (year, month, day, hour, minute, second, millisecond);
}

}

/*prototype extends methods*/
DateTime.prototype.IsLeapYear = function (year) {
If ((year% 4 = 0) && (year% 100!= 0)) | | (Year% 400 = 0))
return true;
return false;
}


DateTime.prototype.DaysInMonth = function (year, month) {
var monthdays = new Array (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
var monthdaysleapyear = new Array (0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
if (Datetime.isleapyear (year)) {
return Monthdaysleapyear[month];
}
else {
return Monthdays[month];
}
}

DateTime.prototype.Pad = function (number) {
Return (number < 10?) ' 0 ': ') + number;
}

/* . NET ' s TimeSpan class*/
function TimeSpan () {

this.milliseconds = 0;

Switch (arguments.length) {
Case 1:
This.milliseconds = Arguments[0];
Break
Default
Throw ("No constructor");
}

This.totaldays = function () {
Return this.milliseconds/(24 * 3600 * 1000);
}

This.totalhours = function () {
return this.milliseconds/(3600 * 1000);
}

This.totalminutes = function () {
Return this.milliseconds/(60 * 1000);
}

This.totalseconds = function () {
return this.milliseconds/1000;
}

This.totalmilliseconds = function () {
return this.milliseconds;
}
}
How to use it?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.