隨筆 - 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 飛翔天使 閱讀(2217) 評論(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("星期四")  回復  更多評論
            
          主站蜘蛛池模板: 龙川县| 宝坻区| 凤山市| 杨浦区| 四会市| 广汉市| 会昌县| 噶尔县| 仪征市| 彭山县| 静宁县| 新平| 泰顺县| 饶平县| 炉霍县| 昔阳县| 桃园市| 榆社县| 静海县| 景泰县| 丹东市| 探索| 佛坪县| 云梦县| 凌海市| 武鸣县| 唐山市| 禹州市| 金平| 淮安市| 梅州市| 冀州市| 宣恩县| 达孜县| 金华市| 桓台县| 灵山县| 贵州省| 塔河县| 南陵县| 庆安县|