也许是个老掉牙的问题,可实在没办法,正则就是学不会,败给他了。
nDate = new Date().toDateString();//显示为:Wed Aug 12 2009
可是想显示成'<span style="color:#009900">['+new Date().toDateString().substr(0,3)+'.]</span>'+new Date().toDateString().substr(3,4)+'.'+new Date().toDateString().substr(7)
笨办法:'<span style="color:#009900">['+nDate.substr(0,3)+'.]</span>'+nDate.substr(3,4)+'.'+nDate.substr(7)
正则是否简便些?

解决方案 »

  1.   

    很抱歉,你给的那段代码都不能执行············是否想给时间加颜色???正则我是菜鸟·······不过还有很多方法可以实现啊而且,你竟然还用 substr()来获取日期的各个部分  - -nDate = new Date();nDate.GetFullYear();//获得年
    nDate.GetMonth();//月
    ··························
      

  2.   

    星期和月份都可以自由读取了,至于变色自己处理吧
    L@_@K
    <!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>
      <title>js.date.prototypeExtesion.html</title>
      <meta name="generator" content="editplus" />
      <meta name="author" content="[email protected]" />
      <meta name="keywords" content="" />
      <meta name="description" content="" />
     </head> <body>
      <script type="text/javascript">
      <!--
    Date.prototype.getMonthAbbr = function() {
    var monthAbbrs = ["Jan.", "Feb.", "Mar.", "Apr.", "May.", "Jun.", "Jul.", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."];
    return monthAbbrs[this.getMonth()];
    };Date.prototype.getWeekDayAbbr = function() {
    var weekDayAbbrs = ["Sun.", "Mon.", "Tue.", "Wed.", "Thu.", "Fri.", "Sat."];
    return weekDayAbbrs[this.getDay()];
    };document.write('<span style="color:#009900">['+new Date().toDateString().substr(0,3)+'.]</span>'+new Date().toDateString().substr(3,4)+'.'+new Date().toDateString().substr(7)+"<br />");var nDate = new Date();
    document.write(nDate.getWeekDayAbbr(), " ", nDate.getMonthAbbr(), " ",nDate.getDate(), " ", nDate.getFullYear());
      //-->
      </script>
     </body>
    </html>
      

  3.   

    星期和月份都可以自由读取了,至于变色自己处理吧
    L@_@K
    <!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>
      <title>js.date.prototypeExtesion.html</title>
      <meta name="generator" content="editplus" />
      <meta name="author" content="[email protected]" />
      <meta name="keywords" content="" />
      <meta name="description" content="" />
     </head> <body>
      <script type="text/javascript">
      <!--
    Date.prototype.getMonthAbbr = function() {
    var monthAbbrs = ["Jan.", "Feb.", "Mar.", "Apr.", "May.", "Jun.", "Jul.", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."];
    return monthAbbrs[this.getMonth()];
    };Date.prototype.getWeekDayAbbr = function() {
    var weekDayAbbrs = ["Sun.", "Mon.", "Tue.", "Wed.", "Thu.", "Fri.", "Sat."];
    return weekDayAbbrs[this.getDay()];
    };document.write('<span style="color:#009900">['+new Date().toDateString().substr(0,3)+'.]</span>'+new Date().toDateString().substr(3,4)+'.'+new Date().toDateString().substr(7)+"<br />");var nDate = new Date();
    document.write(nDate.getWeekDayAbbr(), " ", nDate.getMonthAbbr(), " ",nDate.getDate(), " ", nDate.getFullYear());
      //-->
      </script>
     </body>
    </html>
      

  4.   

    星期和月份都可以自由读取了,至于变色自己处理吧
    L@_@K
    <!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>
      <title>js.date.prototypeExtesion.html</title>
      <meta name="generator" content="editplus" />
      <meta name="author" content="[email protected]" />
      <meta name="keywords" content="" />
      <meta name="description" content="" />
     </head> <body>
      <script type="text/javascript">
      <!--
    Date.prototype.getMonthAbbr = function() {
    var monthAbbrs = ["Jan.", "Feb.", "Mar.", "Apr.", "May.", "Jun.", "Jul.", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."];
    return monthAbbrs[this.getMonth()];
    };Date.prototype.getWeekDayAbbr = function() {
    var weekDayAbbrs = ["Sun.", "Mon.", "Tue.", "Wed.", "Thu.", "Fri.", "Sat."];
    return weekDayAbbrs[this.getDay()];
    };document.write('<span style="color:#009900">['+new Date().toDateString().substr(0,3)+'.]</span>'+new Date().toDateString().substr(3,4)+'.'+new Date().toDateString().substr(7)+"<br />");var nDate = new Date();
    document.write(nDate.getWeekDayAbbr(), " ", nDate.getMonthAbbr(), " ",nDate.getDate(), " ", nDate.getFullYear());
      //-->
      </script>
     </body>
    </html>
      

  5.   

    抱歉抱歉,其实是对一个字符串 Wed Aug 12 2009 的修饰,变成 [Wed.] Aug. 12 2009 ,其中 [Wed.] 改变颜色。只是想更加简便,正则应该可以做到。其他比较繁琐的方法就算了。多谢啊!