js,同一段时间代码,其他人的没事,可以正常返回时间,而我的就返回NAN

求真挚意 2017-08-09 09:37:17
<html>
<head>
<script language="javascript">
function Window_Load(){
var str = "Tue Jul 16 01:07:00 CST 2013";
alert(formatCSTDate(str,"yyyy-M-d")); //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>
...全文
236 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
说明你那台服务器使用的浏览器不支持 var str = "Tue Jul 16 01:07:00 CST 2013"; 这种格式的日期,转换不成正确的Date实例调用方法返回的就是NaN,传递给formatCSTDate前自己用正则什么的格式化为 年/月/日 HH:mm:ss这种格式试试
求真挚意 2017-08-09
  • 打赏
  • 举报
回复
在其他服务器上测试的没问题,但是在另外一个服务器上同一段代码,就出现了NAN的问题。

87,990

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧