POPLX_杀手 2015-06-11 00:59 采纳率: 80%
浏览 1768
已采纳

这段字符串如何取出想要的东西?

var A= "2015-06-05 16:21:40";这个A只是这个格式,还可能是"2015-11-21 12:23:23"

例如我想取出:B="2015-06"; C="06-05";

  • 写回答

5条回答 默认 最新

  • danielinbiti 2015-06-11 01:09
    关注
    <!DOCTYPE html>
    <html>
    <body>
    <script>
    Date.prototype.Format = function (fmt) { //author: meizz 
        var o = {
            "M+": this.getMonth() + 1, //月份 
            "d+": this.getDate(), //日 
            "h+": this.getHours(), //小时 
            "m+": this.getMinutes(), //分 
            "s+": this.getSeconds(), //秒 
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
            "S": this.getMilliseconds() //毫秒 
        };
        if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    }
    var B= new Date('2012-01-01 16:53:24').Format("MM-dd");
    var C= new Date('2012-01-01 16:53:24').Format("yyyy-MM");
    alert(B+','+C);
    </script>
    
    </body>
    </html>
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?