js如何将日期型转成字符串?

比如"2016-06-06 12:05:35"变成"20160606120535"??

replace去掉-空白和:不就行了

 var s='2016-06-06 12:05:35'
s=s.replace(/[- :]/g,'')

日期对象要调用api,然后格式化下


    function addZero(v){if(v<10)return '0'+v;return v.toString()}
    var d = new Date();
    var s = d.getFullYear().toString() + addZero(d.getMonth() + 1) + addZero(d.getDate()) + addZero(d.getHours()) + addZero(d.getMinutes()) + addZero(d.getSeconds());
    alert(s)

用SimpleDateFormate格式化时间,你可以百度下!

 给你个例子 
        Date date= new Date();//创建一个时间对象,获取到当前的时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置时间显示格式
        String str = sdf.format(date);//将当前时间格式化为需要的类型

一个笨办法:
Date date=new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = format.format(date);
str=str.replaceAll(" ", "").replaceAll("-", "").replaceAll(":", "");

这写的都是js吗,好像不是js吧

设置var 年 月 日 时 分 秒 都为0,然后get到相应的数据拼接,判断下大于10直接拼接,小于10就在前面加个0,最后toString就会得到你想要的字符串了

var date = new Date();

var time = date.toString();
如果你想要获取年月日的具体值可以用split函数转化为数组,即
var date = new Date();

var time = date.toString().split(" ");

送你一段代码

Date.prototype.format = function(format) {
    var o = {
        "M+" : this.getMonth() + 1, // month
        "d+" : this.getDate(), // day
        "h+" : this.getHours(), // hour
        "m+" : this.getMinutes(), // minute
        "s+" : this.getSeconds(), // second
        "q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
        "S" : this.getMilliseconds()
    // millisecond
    };
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for ( var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
};

alert(new Date().format("yyyyMMddhhmmss"))

回答真多啊!提供个方法参考,替换非数字部分为空

var str = "2016-06-06 12:05:35";
str = str.replace(/[^\d]/g,"");
console.log(str);// => 20160606120535

如果要转换的参数是日期类型的直接用:
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = format.format(date);
如果是拼接起来的日期格式参数,楼上的这个就可以:
var str = "2016-06-06 12:05:35";
str = str.replace(/[^\d]/g,"");
console.log(str);// => 20160606120535

Date.prototype.format = function(format) {
var o = {
"M+" : this.getMonth() + 1, // month
"d+" : this.getDate(), // day
"h+" : this.getHours(), // hour
"m+" : this.getMinutes(), // minute
"s+" : this.getSeconds(), // second
"q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
"S" : this.getMilliseconds()
// millisecond
};
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for ( var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
};

alert(new Date().format("yyyyMMddhhmmss"))这个好

http://blog.csdn.net/w172087242/article/details/51262209

我喜欢用正则表达式:
var str = "2016-06-06 12:05:35";
str = str.replace(/[^\d]/g,"");
console.log(str);// => 20160606120535
但是这个地方用正则有点小材大用了,还是用日期函数吧:
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String str = format.format(date);

你的头像这么调皮?看到我差点流口水

str.replace(/[^\d]/g,'')