移动端 js new Date 根据日期获取日期问题 Invalid date 、undefined,
$(function(){
var date="2017-10-04";
function getweek(date){
var week;
if(date.getDay()==0) week="星期日"
if(date.getDay()==1) week="星期一"
if(date.getDay()==2) week="星期二"
if(date.getDay()==3) week="星期三"
if(date.getDay()==4) week="星期四"
if(date.getDay()==5) week="星期五"
if(date.getDay()==6) week="星期六"
return week;
}
alert(getweek(new Date(date)));
})
如上,可以根据日期,得到星期。
问题来了。在移动端测试的时候,返回的结果有时会出现undefined,
new Date(date)会返回一个Invalid date
解决办法:
getweek(new Date(date.replace(/-/g,"/")))
将日期由"2017-10-04"替换成"2017/10/04"
文章来自:https://blog.csdn.net/qq_15058425/article/details/78717372