日期相减如何兼容IE?

var oDate=new Date();//今天
var oYear=oDate.getFullYear();//取得今年年份
var oMonth=oDate.getMonth();//取得今月月份
var fDate=new Date(oYear+','+(oMonth+1)+','+1);//取得本月第一天
var nMonth=new Date(oYear+','+(oMonth+2)+','+1);//取得下个月第一案
var lMonth=new Date(oYear+','+(oMonth)+','+1);//取得上个月第一天
var firstPos=fDate.getDay()-1;//1号在日历中的位置
var MonthDay=(nMonth-fDate)/1000/60/60/24;//这个月有几天 -->这里IE浏览器的结果是NAN
var lastMonthDay=(fDate-lMonth)/1000/60/60/24;//上个月有几天 -->这里IE浏览器的结果是NAN

日期做减法如何兼容IE?
网上找了半天也没有一个满意的答案,求大神赐教。


慕码人2483693
浏览 747回答 1
1回答

九州编程

new Date(oYear+','+(oMonth+1)+','+1); 不需要转换成字符串new Date(年, 月, 日, 时, 分, 秒, 毫秒)var oDate=new Date();//今天var oYear=oDate.getFullYear();//取得今年年份var oMonth=oDate.getMonth();//取得今月月份//取得本月第一天var fDate=new Date(oYear+','+(oMonth+1)+','+1);//IE中为 NaNvar fDate=new Date(oYear, (oMonth+1), 1);//取得下个月第一案var nMonth=new Date(oYear+','+(oMonth+2)+','+1);//IE中为 NaNvar nMonth=new Date(oYear, (oMonth+2), 1);//取得上个月第一天var lMonth=new Date(oYear+','+(oMonth)+','+1);//IE中为 NaNvar lMonth=new Date(oYear, (oMonth), 1);//1号在日历中的位置var firstPos=fDate.getDay()-1;var MonthDay=(nMonth-fDate)/1000/60/60/24;//这个月有几天 -->IE中正常var lastMonthDay=(fDate-lMonth)/1000/60/60/24;//上个月有几天 -->IE中正常
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript