js日期格式转换Wed Mar 22 13:38:37 CST 2017 转换 为yyyy-mm-dd

js日期格式转换:Wed Mar 22 13:38:37 CST 2017 转换 为yyyy-mm-dd

<html>
<head>test</head>
<body>
  <p>js日期格式转换:Wed Mar 22 13:38:37 CST 2017 转换 为yyyy-mm-dd</p>
  <script>
  function todate(inputstr, showsplit, showweek) {
        //Wed Mar 22 13:38:37 CST 2017
        inputstr = inputstr + ""; //末尾加一个空格
        var date = "";
        var month = new Array();
        var week = new Array();
        
        month["Jan"] = 1; month["Feb"] = 2; month["Mar"] = 3; month["Apr"] = 4; month["May"] = 5; month["Jan"] = 6; 
        month["Jul"] = 7; month["Aug"] = 8; month["Sep"] = 9; month["Oct"] = 10; month["Nov"] = 11; month["Dec"] = 12;
        week["Mon"] = "一"; week["Tue"] = "二"; week["Wed"] = "三"; week["Thu"] = "四"; week["Fri"] = "五"; week["Sat"] = "六"; week["Sun"] = "日";
        
        str = inputstr.split(" ");
        
        date = str[5];
        date += showsplit + month[str[1]] + showsplit + str[2];
        if(showweek){
        date += "    " + " 星期" + week[str[0]];
        }
                
        return date;
    }


    var a="Wed Mar 22 13:38:37 CST 2017";
    var b=new Date(a);
    var c=todate(a, "-", true);
    var d=todate(a, "", false);
    
    alert(b); //Thu Mar 23 2017 03:38:37 GMT+0800 (中国标准时间)
    alert(c); //2017-3-22 星期三
    alert(d); //2017322
  </script>
</body>
</html>

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页