最好能在数据库查询的时候就转了,前端处理的话,没有专门的方法,得自己去写:
<html><head>
<script language="javascript">
function Window_Load(){
var str = "Tue Jul 16 01:07:00 CST 2013"
alert(formatCSTDate(str,"yyyy-M-d hh:mm:ss")) //2013-7-16 16:24:58
alert(formatDate((new Date()),"yyyy-MM-dd")) //2013-07-15
alert(formatDate((new Date()),"yyyy/M/d")) //2013/7/15
}
//格式化CST日期的字串
function formatCSTDate(strDate,format){
return formatDate(new Date(strDate),format)
}
//格式化日期,
function formatDate(date,format){
var paddNum = function(num){
num += ""
return num.replace(/^(\d)$/,"0$1")
}
//指定格式字符
var cfg = {
yyyy : date.getFullYear() //年 : 4位
,yy : date.getFullYear().toString().substring(2)//年 : 2位
,M : date.getMonth() + 1 //月 : 如果1位的时候不补0
,MM : paddNum(date.getMonth() + 1) //月 : 如果1位的时候补0
,d : date.getDate() //日 : 如果1位的时候不补0
,dd : paddNum(date.getDate())//日 : 如果1位的时候补0
,hh : date.getHours() //时
,mm : date.getMinutes() //分
,ss : date.getSeconds() //秒
}
format || (format = "yyyy-MM-dd hh:mm:ss")
return format.replace(/([a-z])(\1)*/ig,function(m){return cfg[m]})
}
</script>
</head>
<body onload="Window_Load()">
</body>
</html>
参考代码如下:
var s='2017-05-24 12:33:22''定义日期字符串s=s.replace(/ \d+(:\d+){2}/,'')'正则过滤后面的时间,只显示年月日
alert(s) '弹出日期
效果图如下:
定义和用法
replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串。
返回值
一个新的字符串,是用 replacement 替换了 regexp 的第一次匹配或所有匹配之后得到的。
Date 有个toJSON方法,现在大部分环境都可用的。它的结果是类似
距离YYYYMMDD格式最接近,但是toJSON没有根据时区打印,有个取巧的方法
把 T 和 Z 和毫秒去掉就正好是 YYYY-MM-DD H:i:s
https://ishowshao.com/