javascript datetime時間操作函數

來源:互聯網
上載者:User

網頁特效 datetime時間操作函數
html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>DateTime and TimeSpan </title>
  <style>
      body {screen.css教程 (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>
  </head>
 
  <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(20)", 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>
  </html>

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 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 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 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 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 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 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;
    }
}
 如何使用?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.