在JavaScript中,会不会有像在msSQL那样格式花时间表达的方法存在?

解决方案 »

  1.   

    JS区分大小写你里面一个是mm一个是MM
      

  2.   

    to benjaminwu198818:
    js里没mssql那么方便to chinmo :
    我上面有作大小写处理,我这些代码没有错,只是想问一下,如何实现这样形式的写法:alert( myDate("2009-03-26 12:30:00").toString("yyyy-mm-dd") ) 
      

  3.   

    http://blog.csdn.net/yzy0612/archive/2007/08/07/1730732.aspx自己看别人的吧
    看别人写的格式化
      

  4.   

    var myDate = function(d){
    myDate._d=new Date(d.replace(/-/g, "/"));
    return myDate
    }; //new Object();
    myDate.toString = function( format ){
        if(format && typeof format == "string")
            format = format.toLowerCase();    switch(format) {
            case "yyyy-mm-dd hh:mm:ss" :
                return "2009-03-26 12:00:00"
            case "yyyy-mm-dd hh:mm" :
                return "2009-03-26 12:00"
            case "yyyy-mm-dd" :
                return "2009-03-26"
            case "yyyy" :
                return "2009"
            case "mm" :
                return myDate._d.getMonth() + 1;
            default :
                return "2009-03-26 12:00:00"
        }
    }alert(myDate("2009-03-12").toString("mm"))
      

  5.   

    嗯,传递不了,重写Date对象的toString()方法
    不知道楼主的代码不要new object()而用new date()行不行?
      

  6.   

    var myDate = function(d){
    d=new Date(d.replace(/-/g, "/"));

    var f={};
    f.toString = function( format ){
    if(format && typeof format == "string")
    format = format.toLowerCase();

    switch(format) {
    case "yyyy-mm-dd hh:mm:ss" :
    return "2009-03-26 12:00:00"
    case "yyyy-mm-dd hh:mm" :
    return "2009-03-26 12:00"
    case "yyyy-mm-dd" :
    return "2009-03-26"
    case "yyyy" :
    return "2009"
    case "mm" :
    return d.getMonth() + 1;
    default :
    return "2009-03-26 12:00:00"
    }
    }

    return f;
    }; //new Object();
    alert(myDate("2009-03-12").toString("mm"))这就能解决
      

  7.   

    学习 http://blog.csdn.net/yzy0612/archive/2007/08/07/1730732.aspx 
      

  8.   

    to silentwins :
        扩展系统的Date,我开始有想到过,后来感觉还是另外再写。to chinmo :
      您给的别人格式化那种方式,我见过,是一种不错的思路。to cloudgamer :
      第一种方法感觉不错
        return myDate后不需要用prototype定义function
       单例有什么不好吗??????? (显然单例模式的要点有三个;一是某各类只能有一个实例;二是它必须自行创建这个事例;三是它必须自行向整个系统提供这个实例。)  您的第二种方法也可以实现我说的效果,但如果以后要继续扩展属性,方法,感觉就没办法了????
      

  9.   

    to cloudgamer :哦,明白,2种方式都可以加扩展,如:
    var myDate = function(d){
        myDate._d=new Date(d.replace(/-/g, "/"));
        return myDate
    }; //new Object();
    myDate.toString = function( format ){
        if(format && typeof format == "string")
            format = format.toLowerCase();    switch(format) {
            case "yyyy-mm-dd hh:mm:ss" :
                return "2009-03-26 12:00:00"
            case "yyyy-mm-dd hh:mm" :
                return "2009-03-26 12:00"
            case "yyyy-mm-dd" :
                return "2009-03-26"
            case "yyyy" :
                return "2009"
            case "mm" :
                return myDate._d.getMonth() + 1;
            default :
                return "2009-03-26 12:00:00"
        }
    }//新加的
    myDate.fun=function(){
    return myDate._d;
    }alert(myDate("2009-03-12").toString("mm"))
    alert(myDate("2009-03-12").fun())var myDate = function(d){
        d=new Date(d.replace(/-/g, "/"));
        
        var f={};
        f.toString = function( format ){
            if(format && typeof format == "string")
                format = format.toLowerCase();
        
            switch(format) {
                case "yyyy-mm-dd hh:mm:ss" :
                    return "2009-03-26 12:00:00"
                case "yyyy-mm-dd hh:mm" :
                    return "2009-03-26 12:00"
                case "yyyy-mm-dd" :
                    return "2009-03-26"
                case "yyyy" :
                    return "2009"
                case "mm" :
                    return d.getMonth() + 1;
                default :
                    return "2009-03-26 12:00:00"
            }
        }
    //新加的
    f.fun = function(){
    return d
    }
        
        return f;
    }; //new Object();alert(myDate("2009-03-12").toString("mm"))
    alert(myDate("2009-03-12").fun())   但这样的问题是只能单例
        ----------------------------
        按你的意思是这儿用单例不好,为什么不好呢,麻烦你讲解一下??
      

  10.   

    单例这里会出问题的
    var t1 = myDate("2009-03-12");
    var t2 = myDate("2009-04-12");
    alert(t1.toString());呵呵,cloudgamer的代码很好的解决了楼主的问题,不过每都是一个新的toStringfunction太浪费了
    我也来贴一种实现方式,<script type="text/javascript">(function(){
    var myDate = window.myDate = function(d){
        
         return new fn.init(d);
    }; //new Object();

    var fn = myDate.prototype = {
    init: function(d){
    this.date = new Date(d.replace(/-/g, "/"));
    },
    toString: function( format ){
    if(format && typeof format == "string")
    format = format.toLowerCase();

    switch(format) {
    case "yyyy-mm-dd hh:mm:ss" :
    return "2009-03-26 12:00:00"
    case "yyyy-mm-dd hh:mm" :
    return "2009-03-26 12:00"
    case "yyyy-mm-dd" :
    return "2009-03-26"
    case "yyyy" :
    return "2009"
    case "mm" :
    return this.date.getMonth() + 1;
    default :
    return "2009-03-26 12:00:00"
    }
         },
    valueOf:function(){  
    return this.date.valueOf();
    }
    }
    fn.init.prototype = fn;
    })();var myDate1 = new myDate("2009-03-12");    //新建对象的方式1 
    var myDate2 = myDate("2009-04-12");   //新建对象的方式2
      
    alert(myDate1.toString("mm"));
    alert(myDate2.valueOf());
    </script>
      

  11.   

    谢谢hzrui :
    你代码里的myDate.prototype = 是不是可以不要,我去掉显示结果一样,不知道你的用意fn.init.prototype = fn;
    这句不明白意思?
      

  12.   

    myDate.prototype是可以不要fn.init.prototype = fn; fn是一个obj
    fn.init是一个function, function.prototype = obj 仅此而已
      

  13.   

    用new也行
    var myDate = function(d){   
        return new f(d);
    }; //new Object();function f(d){
    this._d=new Date(d.replace(/-/g, "/"));
    }
    f.prototype.toString = function( format ){
    if(format && typeof format == "string")
    format = format.toLowerCase(); switch(format) {
    case "yyyy-mm-dd hh:mm:ss" :
    return "2009-03-26 12:00:00"
    case "yyyy-mm-dd hh:mm" :
    return "2009-03-26 12:00"
    case "yyyy-mm-dd" :
    return "2009-03-26"
    case "yyyy" :
    return "2009"
    case "mm" :
    return this._d.getMonth() + 1;
    default :
    return "2009-03-26 12:00:00"
    }
    }alert(myDate("2009-03-12").toString("mm"))
      

  14.   

    那实质上就是lz的方法2了
    只是把new放到函数里面
      

  15.   

    这样更好
    var myDate = function(d){
        d=new Date(d.replace(/-/g, "/"));
        
        var f={};
        f.toString = function( format ){ return ff(format,d) }

        return f;
    }; //new Object();function ff(format,d){
            if(format && typeof format == "string")
                format = format.toLowerCase();
        
            switch(format) {
                case "yyyy-mm-dd hh:mm:ss" :
                    return "2009-03-26 12:00:00"
                case "yyyy-mm-dd hh:mm" :
                    return "2009-03-26 12:00"
                case "yyyy-mm-dd" :
                    return "2009-03-26"
                case "yyyy" :
                    return "2009"
                case "mm" :
                    return d.getMonth() + 1;
                default :
                    return "2009-03-26 12:00:00"
            }
    }alert(myDate("2009-03-12").toString("mm"))
      

  16.   

    用String对象的substr()和substring()…提取字符串,这样子不知道能不能…符合楼主的意思
      

  17.   

    谢谢各位。
    我对于js里的闭包,原型...的东西感觉好模糊的,似懂非懂,得抽空系统的好好学习一下。我本来是在asp.net里用DataSet的GetXml把数据库的记录转换成xml,然后在js代码里用ajax调用时,把日期类型的按自己的要求去显示。可结果发现日期返回这样的格式2009-01-20T16:52:37.03+08:00(也可能日期为""),后来没办法,只有在js里加上处理,用正则把"T"替换掉,把秒后面的.及其后面的数据去掉,也不知道有没有其他好办法(服务器端asp.net的代码不考虑修改)
    观望一下,计划明天结帖...
      

  18.   

    51js上有人给出跟22楼类似的代码:<script type="text/javascript">
    //<![CDATA[
    function myDate(text){
    return new MyDate(text);
    }var MyDate = function( text ){
    this.rat = new String(text);
    this.rat = new Date(this.rat.replace(/-/g, "/"));
    this.isDateFlag = ! isNaN(this.rat); this.toString = function( format ){
    if(! this.isDateFlag)
    return ""; if(format && typeof format == "string")
    format = format.toLowerCase(); switch(format) {
    case "yyyy-mm-dd hh:mm:ss" :
    return this.rat.getFullYear()
    + "-" + (this.rat.getMonth()<9 ? "0" : "") + (this.rat.getMonth() + 1) 
    + "-" + (this.rat.getDate()<10 ? "0" : "") + this.rat.getDate() 
    + " " + (this.rat.getHours()<10 ? "0" : "") + this.rat.getHours() 
    + ":" + (this.rat.getMinutes()<10 ? "0" : "") + this.rat.getMinutes() 
    + ":" + (this.rat.getSeconds()<10 ? "0" : "") + this.rat.getSeconds();
    case "yyyy-mm-dd hh:mm" :
    return this.rat.getFullYear()
    + "-" + (this.rat.getMonth()<9 ? "0" : "") + (this.rat.getMonth() + 1) 
    + "-" + (this.rat.getDate()<10 ? "0" : "") + this.rat.getDate() 
    + " " + (this.rat.getHours()<10 ? "0" : "") + this.rat.getHours() 
    + ":" + (this.rat.getMinutes()<10 ? "0" : "") + this.rat.getMinutes();
    case "yyyy-mm-dd" :
    return this.rat.getFullYear()
    + "-" + (this.rat.getMonth()<9 ? "0" : "") + (this.rat.getMonth() + 1) 
    + "-" + (this.rat.getDate()<10 ? "0" : "") + this.rat.getDate();
    case "yyyy" :
    return this.rat.getFullYear();
    case "mm" :
    return (this.rat.getMonth()<9 ? "0" : "") + (this.rat.getMonth() + 1);
    default :
    return this.rat.getFullYear()
    + "-" + (this.rat.getMonth()<9 ? "0" : "") + (this.rat.getMonth() + 1) 
    + "-" + (this.rat.getDate()<10 ? "0" : "") + this.rat.getDate() 
    + " " + (this.rat.getHours()<10 ? "0" : "") + this.rat.getHours() 
    + ":" + (this.rat.getMinutes()<10 ? "0" : "") + this.rat.getMinutes() 
    + ":" + (this.rat.getSeconds()<10 ? "0" : "") + this.rat.getSeconds();
    }
    }

    //return this.rat;
    }
    alert( myDate("2009-03-26 12:30:00").toString() );
    alert( myDate("2009-03-26 12:30:00").toString("yyyy-mm-dd") );//]]>
    </script>
      

  19.   

    我不是来和你讨论单例不单例,对象不对象的,我想说两个额外的问题:
    1:你的mm既表示月份,也表示分钟,是不好的,分钟就用nn更好,以免混淆。
    2:代码老长老长的,我不喜欢,简洁而不简单,是JS人的追求。俺给你整个精简的,功能还更强:function myDate(text){
        return new MyDate2(text);  //换了个不同的名字MyDate2,省得有故意引起混淆的嫌疑。
    }
    var MyDate2 = function( text ){
        var d = new Date(text.replace(/-/g, "/"));
        this.toString = function( format ){
            return isNaN(d) ? "" : (format ? format.toLowerCase() : "yyyy-mm-dd hh:nn:ss").replace(/yyyy/g, d.getFullYear()).replace(/mm/g, d.getMonth()+1).replace(/dd/g, d.getDate()).replace(/hh/g, d.getHours()).replace(/nn/g, d.getMinutes()).replace(/ss/g, d.getSeconds()).replace(/(?=[^\d])(\d)(?=[^\d])/g,'0$1');
        }
    }
    //format的格式随便你写啦,有yyyy,mm,dd,hh,nn,ss的地方就替换掉。
    document.writeln( myDate("2009-03-26 12:30:00").toString() );
    document.writeln("<br>");
    document.writeln( myDate("2009-03-23 12:30:00").toString("yyyy-mm-dd") );
    document.writeln("<br>");
    document.writeln( myDate("2009-03-23 12:30:00").toString("yyyy年mm月dd日") );
      

  20.   

    csdn的代码不能自动换行?中间有空格也不换啊?
      

  21.   

    看了一下,toString是写的比较具体,但是我觉得代码封装性还不如我的简洁明了,#18
      

  22.   

    无聊凑个热闹,我人比较笨,N久以前用的字符串操作,呵呵....再加new Date()的构造函数来判断日期格式....
    var temp = "20009-1-2 12:13:14";//或者不带日期"2009-2-3"
    //replace(/-/g,"/");之前想过用这个,但是加了时间貌似就不能new Date()了......
    this.d = new Date(parseInt(temp.split("-")[0]),parseInt(temp.split("-")[1])-1,parseInt(temp.substring(temp.lastIndexOf("-")+1,temp.indexOf(" "))),parseInt(temp.substring(temp.indexOf(" ")+1,temp.indexOf(":"))),parseInt(temp.split(":")[1]),parseInt(temp.split(":")[2]));if(isNaN(this.d.getFullYear())){
    this.d = new Date(parseInt(temp.split("-")[0]),parseInt(temp.split("-")[1])-1,parseInt(temp.split("-")[2]));
    if(isNaN(this.d.getFullYear())){
    this.d = new Date();//2种都不匹配,我才赋值当前时间
    }
    }
    }js的new Date()可以带的参数是:
    "month dd,yyyy hh:mm:ss"    如:"May 12,2009 11:12:13"
    "month dd,yyyy"             如:"January 12,2009"
    yyyy,MM,dd,hh,mm,ss    太常见了,不用说了
    yyyy,MM,dd
    ms                    1970年1月1日到某个时间的毫秒数
      

  23.   

    谢谢cuixiping 的指点
    谢谢各位...结帐...