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

          常用鏈接

          留言簿(1)

          隨筆分類(146)

          隨筆檔案(147)

          文章分類(28)

          文章檔案(28)

          喜歡的Blog

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          Javascript實現(xiàn)日期星期全顯示,項目的需要顯示日期和星期,下面代碼就可以顯示當前的日期和星期
          <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”操作符。在這里,就是創(chuàng)建了日期對象。

          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實現(xiàn)日期星期的顯示 2009-04-07 09:06 haix
          不錯,支持
          firefox那  回復  更多評論
            
          # re: javascript實現(xiàn)日期星期的顯示 2010-01-21 10:56 黑葉拜天
          新手!學習啦~
          謝謝呀  回復  更多評論
            
          # re: javascript實現(xiàn)日期星期的顯示 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("星期四")  回復  更多評論
            
          主站蜘蛛池模板: 玉树县| 同心县| 清丰县| 牡丹江市| 阿拉善盟| 丘北县| 盐池县| 芷江| 米脂县| 南华县| 溆浦县| 望城县| 遂溪县| 睢宁县| 高尔夫| 获嘉县| 沙河市| 锦屏县| 汾西县| 云浮市| 红河县| 鄂尔多斯市| 三台县| 左权县| 新民市| 海原县| 乌拉特后旗| 会昌县| 都匀市| 永胜县| 抚顺县| 呼伦贝尔市| 荣成市| 鹤山市| 拉孜县| 灵川县| 汾西县| 马公市| 惠来县| 夏邑县| 星座|