隨筆 - 147  文章 - 71  trackbacks - 0
          <2009年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          常用鏈接

          留言簿(1)

          隨筆分類(146)

          隨筆檔案(147)

          文章分類(28)

          文章檔案(28)

          喜歡的Blog

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          Javascript實現日期星期全顯示,項目的需要顯示日期和星期,下面代碼就可以顯示當前的日期和星期
          <html>
          <body>
          <script language=javascript>
          <!--
          todayDate 
          = new Date();
          date 
          = todayDate.getDate();
          month
          = todayDate.getMonth() +1;
          year
          = todayDate.getYear();
          document.write(
          "今天是")
          document.write(
          "<br>")
          if(navigator.appName == "Netscape")
          {
              document.write(
          1900+year);
              document.write(
          "");
              document.write(month);
              document.write(
          "");
              document.write(date);
              document.write(
          "");
              document.write(
          "<br>")
          }

          if(navigator.appVersion.indexOf("MSIE"!= -1)
          {
          document.write(year);
          document.write(
          "");
          document.write(month);
          document.write(
          "");
          document.write(date);
          document.write(
          "");
          document.write(
          "<br>")
          }

          if (todayDate.getDay() == 5) document.write("星期五")
          if (todayDate.getDay() == 6) document.write("星期六")
          if (todayDate.getDay() == 0) document.write("星期日")
          if (todayDate.getDay() == 1) document.write("星期一")
          if (todayDate.getDay() == 2) document.write("星期二")
          if (todayDate.getDay() == 3) document.write("星期三")
          if (todayDate.getDay() == 4) document.write("星期四")

          //--> 
          </script>
           
          </body>
          </html>

              todayDate = new Date();

          當定義一個新的對象時,通常使用“new”操作符。在這里,就是創建了日期對象。

          date = todayDate.getDate(); getDate()是Date對象的一種方法,其功能是獲得當前的日期。
          month= todayDate.getMonth() + 1 ;

           getMonth()是Date對象的一種方法,其功能是獲得當前的日期,由于月份是從0開始的,所以這里要“+1”。

          year= todayDate.getYear() getYear()是Date對象的一種方法,其功能是獲得當前的年份。
          if(navigator.appName == "Netscape") { document.write(1900+year); document.write("年"); document.write(month); document.write("月"); document.write(date); document.write("日");
          document.write("<br> ") }
          如果瀏覽器是Netscape,輸出今天是“year”+“年”+“month”+“月”+“date”+“日”,其中年要加1900。
          if(navigator.appVersion.indexOf("MSIE") != -1) { document.write(year); document.write("年"); document.write(month); document.write("月"); document.write(date); document.write("日");
          document.write("<br> ") }
          如果瀏覽器是IE,直接輸出今天是“year”+“年”+“month”+“月”+“date”+“日”。
          if (todayDate.getDay() == 5) document.write("星期五");
          if (todayDate.getDay() == 6) document.write("星期六");
          if (todayDate.getDay() == 0) document.write("星期日");
          if (todayDate.getDay() == 1) document.write("星期一");
          if (todayDate.getDay() == 2) document.write("星期二");
          if (todayDate.getDay() == 3) document.write("星期三");
          if (todayDate.getDay() == 4) document.write("星期四")
          getDay()是Date對象的一種方法,其功能是獲得當前是星期幾。document.write輸出今天是“星期幾”。
          posted on 2009-04-06 10:45 飛翔天使 閱讀(2206) 評論(3)  編輯  收藏 所屬分類: javascript

          FeedBack:
          # re: javascript實現日期星期的顯示 2009-04-07 09:06 haix
          不錯,支持
          firefox那  回復  更多評論
            
          # re: javascript實現日期星期的顯示 2010-01-21 10:56 黑葉拜天
          新手!學習啦~
          謝謝呀  回復  更多評論
            
          # re: javascript實現日期星期的顯示 2014-05-27 15:18 ii
          if (todayDate.getDay() == 5) document.write("星期五");
          if (todayDate.getDay() == 6) document.write("星期六");
          if (todayDate.getDay() == 0) document.write("星期日");
          if (todayDate.getDay() == 1) document.write("星期一");
          if (todayDate.getDay() == 2) document.write("星期二");
          if (todayDate.getDay() == 3) document.write("星期三");
          if (todayDate.getDay() == 4) document.write("星期四")  回復  更多評論
            
          主站蜘蛛池模板: 富源县| 屏南县| 徐闻县| 阳谷县| 凤冈县| 固镇县| 陕西省| 岱山县| 进贤县| 江永县| 荣成市| 德安县| 托克托县| 墨竹工卡县| 读书| 金秀| 宜章县| 建平县| 广饶县| 布尔津县| 临武县| 松潘县| 沙湾县| 扬中市| 喜德县| 定西市| 石景山区| 遂平县| 忻城县| 武隆县| 凉山| 紫阳县| 台南市| 澎湖县| 曲松县| 开远市| 望谟县| 弋阳县| 抚宁县| 香格里拉县| 长阳|