假如日期格式只有1种:
年-月-日 小时:分钟:秒function MyCalendar()
{
this.year=null;
this.month=null;
this.day=null;
this.hour=null;
this.minute=null;
this.second=null;
}如何最有效率的把它们提取到MyCalendar中。

解决方案 »

  1.   

    function getCurTime()
    {
    var dt=new Date();
    alert(dt.getYear()+"年 "+dt.getMonth()+"月 "+dt.getDay()+"日 "+dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds());
    }
    getCurTime();
    </script> 
      

  2.   

    function getCurTime() 

    var dt=new Date(); 
    alert(dt.getYear()+" - "+dt.getMonth()+" - "+dt.getDay()+" "+dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds()); 

    getCurTime(); 
    </script> 
      

  3.   

    在修改了function getCurTime()
    {
    var dt=new Date();
    alert(dt.getYear()+" - "+(dt.getMonth()+1)+" - "+dt.getDate()+"  "+dt.getHours()+":"+dt.getMinutes()+":"+dt.getSeconds());
    }
    getCurTime();
    </script> 
      

  4.   

    var str="2006-09-10 11:12:11"
    var b=str.match(/\d+(?!\d)/g);
    this.year=b[0]; 
    this.month=b[1]; 
    this.day=b[2];  
    this.hour=b[3]; 
    this.minute=b[4]; 
    this.second=b[5]; 
    alert(this.year+"-"+this.month+"-"+this.day+" "+this.hour+":"+this.minute+":"+this.second)
      

  5.   

    如果按照lz所说, 格式确定了的话, 用正则.<html>
    <head>
    <title>用正则</title>
    <script type="text/javascript">
    var date = '2009年05月11日 小时:10分钟:24秒:45 ';
    function MyCalendar(_date) 

    var reg = /\d+/g;
    var timeInfo = _date.match(reg);

    this.year = timeInfo[0]; 
    this.month = timeInfo[1]; 
    this.day = timeInfo[2]; 
    this.hour = timeInfo[3]; 
    this.minute = timeInfo[4]; 
    this.second = timeInfo[5]; 

    MyCalendar(date);
    /*测试用函数
    var mc = new MyCalendar(date);
    alert('year: ' + mc.year + '. month: ' + mc.month + ', day: ' + mc.day + ', hour: ' + mc.hour + ', mimute: ' + mc.minute + ', second: ' + mc.second);
    */
    </script>
    </head>
    <body></body>
    </html>
      

  6.   

    <script language="javascript" type="text/javascript"> 
    function MyCalendar(str) 

    //日期时间的格式 : YYYY-MM-DD hh:mm:ss ;  返回时间的各个部分 ; 
    var dt=str;
    var year=null; 
    var month=null; 
    var day=null; 
    var hour=null; 
    var minute=null; 
    var second=null; 
    year=dt.split("-")[0];
    month=dt.split("-")[1];
    day=(dt.split("-")[2]).substr(0,2);
    hour=dt.split(" ")[1].split(":")[0];
    minute=dt.split(" ")[1].split(":")[1];
    second=dt.split(" ")[1].split(":")[2];
    var r=year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second;
    alert(r);
    } //测试,这里你可以从文本框中得到输入的日期时间数据
    MyCalendar("2009-5-11 10:40:23");
    </script>