隨筆-17  評(píng)論-64  文章-79  trackbacks-1

          有人經(jīng)常問到有關(guān)如何使用JS處理日期的問題,我只好獻(xiàn)拙了。
          希望能夠有點(diǎn)用處。
          1。解決2000問題
          function y2k(number) { return (number < 1000) ? number + 1900 : number; }
          2。檢查日期是否合法
          // 當(dāng)輸入?yún)?shù)為isDate(dd,mm,ccyy)時(shí),表示要檢查年,月,日
          // 當(dāng)輸入?yún)?shù)為isDate(dd,mm) 表示默認(rèn)年為當(dāng)前年
          // 當(dāng)輸入?yún)?shù)為isDate(dd)    表示默認(rèn)年,月為當(dāng)前年月
          // 注意輸入月份保證在1-12以內(nèi)。

          function isDate (day,month,year) {
              var today = new Date();
              year = ((!year) ? y2k(today.getYear())year);
              month = ((!month) ? today.getMonth():month-1);
              if (!day) return false
              var test = new Date(year,month,day); 86oo.com提供各類教程
              if ( (y2k(test.getYear()) == year) &&
                   (month == test.getMonth()) &&
                   (day == test.getDate()) )
                  return true;
              else
                  return false
          }

          以下是調(diào)用例子:
          if (isDate(31,2,1997))
              document.write("Valid");
          else
              document.write("Invalid");

          3。如何判斷兩個(gè)日期中的間隔天數(shù)
          function daysElapsed(date1,date2) {
              var difference = Date.UTC(date1.getYear(),date1.getMonth(),date1.getDate(),0,0,0)
                             - Date.UTC(date2.getYear(),date2.getMonth(),date2.getDate(),0,0,0);
              return difference/1000/60/60/24; 86oo.com
          }

          4。如何將一個(gè)下拉列表框中的月份傳遞到另一頁
          <FORM>
          <SELECT NAME="selectName">
          <OPTION>January
          <OPTION>February
          <OPTION>March
          <OPTION>April
          <OPTION>May
          <OPTION>June
          <OPTION>July
          <OPTION>August
          <OPTION>Spetember
          <OPTION>October
          <OPTION>November
          <OPTION>December
          </SELECT>
          <INPUT TYPE="BUTTON" VALUE="Go" onClick="window.location.href = 'nextpage.html?' +
          this.form.selectName.options[this.form.selectName.selectedIndex].text">
          </FORM>

          在nextpage.html中加入下面的代碼
          <FORM name="formName"><INPUT TYPE="TEXT" name="textName"><FORM>
          <SCRIPT LANGUAGE="JavaScript"><
          document.formName.textName.value = location.search.substring(1);
          //-SCRIPT>

          或則:
          <SCRIPT LANGUAGE="JavaScript"><

          86oo精彩教程


          document.write("<FORM><INPUT TYPE='TEXT' ");
          document.write("VALUE='"location.search.substring(1)+"FORM>")
          //-SCRIPT>

          5。如何將一個(gè)字符串中的時(shí)間和當(dāng)前時(shí)間做比較
          <SCRIPT LANGUAGE="JavaScript">
          /*其中的日期字符串可有以下格式:
             格式 1 : 19970529
             格式 2 : 970529
             格式 3 : 29/05/1997
             格式 4 : 29/05/97
             輸入?yún)?shù)dateType是1到4的數(shù)字,表示使用哪種格式.
          */
          <!-
          function isitToday(dateString,dateType) {
              var now = new Date();
              var today = new Date(now.getYear(),now.getMonth(),now.getDate())

              if (dateType == 1)
                  var date = new Date(dateString.substring(0,4),
                                      dateString.substring(4,6)-1, http://www.86oo.com
                                      dateString.substring(6,8));
              else if (dateType == 2)
                  var date = new Date(dateString.substring(0,2),
                                      dateString.substring(2,4)-1,
                                      dateString.substring(4,6));
              else if (dateType == 3)
                  var date = new Date(dateString.substring(6,10),
                                      dateString.substring(3,5)-1,

          您所瀏覽的文章來自86oo.com


                                      dateString.substring(0,2));
              else if (dateType == 4)
                  var date = new Date(dateString.substring(6,8),
                                      dateString.substring(3,5)-1,
                                      dateString.substring(0,2));
              else
                  return false;

              if (date.toString() == today.toString())
                  return true;
              else
                  return false;

          86oo精彩教程


          }
          調(diào)用的例子如下:
          if (isitToday("19970529",1)) alert('true'); else alert('false');
          if (isitToday("970529",2)) alert('true'); else alert('false');
          if (isitToday("29/05/1997",3)) alert('true'); else alert('false');
          if (isitToday("02/06/97",4)) alert('true'); else alert('false');
          //-
          </SCRIPT>

          6。如何根據(jù)一個(gè)人的生日計(jì)算他的歲數(shù)
          <SCRIPT LANGUAGE="JavaScript"><
          /*其中的日期字符串可有以下格式:
             格式 1 : 19970529
             格式 2 : 970529
             格式 3 : 29/05/1997
             格式 4 : 29/05/97
             輸入?yún)?shù)dateType是1到4的數(shù)字,表示使用哪種格式.
          */
          function getAge(dateString,dateType) {
              var now = new Date();
              var today = new Date(now.getYear(),now.getMonth(),now.getDate())
              var yearNow = now.getYear();
              var monthNow = now.getMonth();
          www.86oo.com

              var dateNow = now.getDate();

              if (dateType == 1)
                  var dob = new Date(dateString.substring(0,4),
                                      dateString.substring(4,6)-1,
                                      dateString.substring(6,8));
              else if (dateType == 2)
                  var dob = new Date(dateString.substring(0,2),
                                      dateString.substring(2,4)-1,
                                      dateString.substring(4,6)); 86oo精彩教程
              else if (dateType == 3)
                  var dob = new Date(dateString.substring(6,10),
                                      dateString.substring(3,5)-1,
                                      dateString.substring(0,2));
              else if (dateType == 4)
                  var dob = new Date(dateString.substring(6,8),
                                      dateString.substring(3,5)-1,
                                      dateString.substring(0,2));

          您所瀏覽的文章來自86oo.com


              else
                  return '';

              var yearDob = dob.getYear();
              var monthDob = dob.getMonth();
              var dateDob = dob.getDate();

              yearAge = yearNow - yearDob;

              if (monthNow > monthDob)
                  var monthAge = monthNow - monthDob;
              else {
                  yearAge--;
                  var monthAge = 12 + monthNow -monthDob;
              }
              if (dateNow > dateDob)
                  var dateAge = dateNow - dateDob;
              else {
                  monthAge--;
                  var dateAge = 31 + dateNow - dateDob; 歡迎各位訪問86oo.com
              }

              return yearAge + ' years ' + monthAge + ' months ' + dateAge + ' days';
          }
          調(diào)用例子
          document.write(getAge("19650104",1)+'BR>')
          document.write(getAge("650104",2)+'BR>')
          document.write(getAge("04/01/1965",3)+'BR>')
          document.write(getAge("04/01/65",4)+'BR>')
          //-SCRIPT>

          7。如何使用下面的格式dd/mm/yy在網(wǎng)頁中顯示日期
          <SCRIPT LANGUAGE = 'JavaScript'>
          <!-
          var date = new Date();
          var d  = date.getDate();
          var day = (d < 10) ? '0' + d : d;
          var m = date.getMonth() + 1;
          var month = (m < 10) ? '0' + m : m;
          var yy = date.getYear();
          var year = (yy < 1000) ? yy + 1900 : yy;

          document.write(day + "/" + month + "/" + year);
          //-
          </SCRIPT>

          8。如何使用下面的格式date month year在網(wǎng)頁中顯示日期
          <SCRIPT LANGUAGE = 'JavaScript'> 86oo精彩教程
          <!-
          function makeArray() {
               for (i = 0; i<makeArray.arguments.length; i++)
                    this[i + 1] = makeArray.arguments[i];
          }

          var months = new makeArray('January','February','March',
              'April','May','June','July','August','September',
              'October','November','December');

          var date = new Date();
          var day  = date.getDate();
          var month = date.getMonth() + 1;
          var yy = date.getYear();
          var year = (yy < 1000) ? yy + 1900 : yy;

          document.write(day + " " + months[month] + " " + year);
          //-
          </SCRIPT>

          9.如何讓我的網(wǎng)頁的最近更新日期更易讀
          <SCRIPT LANGUAGE = 'JavaScript'><
          function makeArray0() {
               for (i = 0; i<makeArray0.arguments.length; i++)
                    this[i] = makeArray0.arguments[i];

          www.86oo.com


          }
          var days = new makeArray0("Sunday","Monday","Tuesday","Wednesday",
              "Thursday","Friday","Saturday");
          var months = new makeArray0('January','February','March',
              'April','May','June','July','August','September',
              'October','November','December');
          function nths(day) {
               if (day == 1 || day == 21 || day == 31)
                    return 'st';
               else
                    if (day == 2 || day == 22)
                         return 'nd';
                         if (day == 3 || day == 23)
                              return 'rd';

          www.86oo.com


                         else return 'th';
          }
          function y2k(number) { return (number < 1000) ? number + 1900 : number; }
          var last = document.lastModified;
          var date = new Date(last);
          document.write("Last updated on " + days[date.getDay()] + ' ' +
              date.getDate() + nths(date.getDate()) + " " +
              months[date.getMonth()] + ", " +
              (y2k(date.getYear()) + "."
          //-SCRIPT>

          10。如何顯示到某個(gè)特定日期的倒記時(shí)
          <SCRIPT LANGUAGE="JavaScript"><
          function y2k(number) { return (number < 1000) ? number + 1900 : number; }

          function timeTillDate(whenDay,whenMonth,whenYear) {
              var now = new Date();
              var thisDay = now.getDate(), thisMonth = now.getMonth() + 1, thisYear = y2k(now.getYear())
              var yearsDifference = whenYear - thisYear, monthsDifference = 0, daysDifference = 0, string = '';
          86oo.com提供各類教程


              if (whenMonth >= thisMonth) monthsDifference = whenMonth - thisMonth;
              else { yearsDifference--; monthsDifference = whenMonth + 12 - thisMonth; }

              if (whenDay >= thisDay)daysDifference = whenDay - thisDay;
              else {
                  if (monthsDifference > 0) monthsDifference--;
                  else { yearsDifference--; monthsDifference+=11; }
                  daysDifference = whenDay + 31 - thisDay;
              }

              if (yearsDifference < 0) return '';

              if ((yearsDifference == 0) && (monthsDifference == 0) && (daysDifference == 0))
                  return '';

              if (yearsDifference > 0) {
                  string = yearsDifference + ' year';

          您所瀏覽的文章來自86oo.com


                  if (yearsDifference > 1) string += 's';
                  string += ' ';
              }

              if (monthsDifference > 0) {
                  string += monthsDifference + ' month';
                  if (monthsDifference > 1) string += 's';
                  string += ' ';
              }
              if (daysDifference > 0) {
                  string += daysDifference + ' day';
                  if (daysDifference > 1) string += 's';
                  string += ' ';
              }

              var difference = Date.UTC(now.getYear(),now.getMonth(),now.getDate(),now.getHours(),now.getMinutes(),now.getSeconds()) - 歡迎各位訪問86oo.com
                               Date.UTC(now.getYear(),now.getMonth(),now.getDate(),0,0,0);

              difference = 1000*60*60*24 - difference;

              var hoursDifference = Math.floor(difference/1000/60/60);
              difference = difference - hoursDifference*1000*60*60
              var minutesDifference = Math.floor(difference/1000/60);
              difference = difference - minutesDifference*1000*60
              var secondsDifference = Math.floor(difference/1000);

              if (hoursDifference > 0) {
                  string += hoursDifference + ' hour';
                  if (hoursDifference > 1) string +='s';
                  string += ' ';
              }
          86oo.com

              if (minutesDifference > 0) {
                  string += minutesDifference + ' minute';
                  if (minutesDifference > 1) string +='s';
                  string += ' ';
              }

              if (secondsDifference > 0) {
                  string += secondsDifference + ' second';
                  if (secondsDifference > 1) string +='s';
                  string += ' ';
              }
              return string;
          }

          調(diào)用例子,例如現(xiàn)在到31/12/1999還有多久。
          document.write(timeTillDate(31,12,1999));
          //-SCRIPT>

          11。如何從一個(gè)日期中減掉幾個(gè)小時(shí)
          <SCRIPT LANGUAGE = 'JavaScript'><
          var date = new Date();
          var date = new Date(Date.UTC(date.getYear(),date.getMonth(),date.getDate(),date.getHours(),date.getMinutes(),date.getSeconds()) - 5*60*60*1000);
          www.86oo.com

          document.write(date);
          //-SCRIPT>

          12。如何在一個(gè)日期中增加幾個(gè)月后并能夠正確顯示出來
          <SCRIPT LANGUAGE="JavaScript"><
          function makeArray() {
              for (i = 0; i<makeArray.arguments.length; i++)
                  this[i + 1] = makeArray.arguments[i];
          }
          var months = new makeArray('January','February','March','April',
                                     'May','June','July','August','September',
                                     'October','November','December');
          function nths(day) {
              if (day == 1 || day == 21 || day == 31) return 'st';
              else if (day == 2 || day == 22) return 'nd'; 86oo.com提供各類教程
              else if (day == 3 || day == 23) return 'rd';
              else return 'th';
          }
          function y2k(number) { return (number < 1000) ? number + 1900 : number; }
          function monthsahead(noofmonths) {
              var today = new Date();
              var date = new Date(today.getYear(),today.getMonth() + noofmonths,today.getDate(),today.getHours(),today.getMinutes(),today.getSeconds())
              return date.getDate() + nths(date.getDate()) + ' ' + months[date.getMonth() + 1] + ' ' + y2k(date.getYear())
          }

          調(diào)用例子:
          document.write(monthsahead(6));
          //-SCRIPT>
          posted on 2007-12-14 15:25 飛鳥 閱讀(963) 評(píng)論(0)  編輯  收藏 所屬分類: AJAX
          主站蜘蛛池模板: 海安县| 凭祥市| 漠河县| 营山县| 绥德县| 九寨沟县| 常山县| 金川县| 宝清县| 兰考县| 波密县| 搜索| 阜城县| 江西省| 龙门县| 化州市| 环江| 通道| 崇信县| 桃园县| 洛浦县| 区。| 普定县| 通化县| 隆林| 大竹县| 山东省| 浦江县| 屏东县| 佛山市| 阿巴嘎旗| 桃园市| 海伦市| 普兰县| 托克托县| 南岸区| 元阳县| 城市| 台前县| 嘉黎县| 宣威市|