我正在使用Date()函数获取当前日期并将其与另一个日期进行比较。
它可以在Android和网络上正常运行,但它无法在iOS上运行。
我的代码是:
function daytogo()
{
var status = document.getElementById('dmy_status').getAttribute('value');
if(status =='C')
{
var event_status="Event has been cancelled";
}
else if(status =='P')
{
var event_status="Event postponed ! Waiting for new date";
}
else
{
/*START OF CALCULATING DATE DIFFERENCE*/
var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds
var a = document.getElementById('dmy_strt').getAttribute('value');
var b = document.getElementById('dmy_end').getAttribute('value');
//var firstDate = new Date(2008,01,12);
// alert(b);
//alert(firstDate);
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today = yyyy+','+mm+','+dd;
// document.write(today);
var firstDate = new Date(a);
var secondDate = new Date(today);
var enddate=new Date(b);
console.log(firstDate);
console.log(secondDate);
// alert(today);
// var diffDays = Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));
// alert(diffDays);
if(firstDate > secondDate)
{
alert("if");
var event_status='';
var diffDays = Math.abs((firstDate.getTime() - secondDate.getTime())/(oneDay));
//alert(diffDays);
if(diffDays <=30 )
{
event_status=diffDays + " days to go";
}
else
{
event_status=(diffDays /30);
event_status=(event_status.toString().split(".")[0])+ ' months to go'; ///before
//alert(event_status.toString().split(".")[1]); ///after
}
}
else if(firstDate <= secondDate && enddate >= secondDate)
{
alert("else if");
var event_status='Ongoing';
}
else
{
alert("else");
var event_status="Event has expired";
//document.getElementById("days_to_go").innerHTML = event_status;
}
//var elements = document.getElementsByClassName('dmy_strt')[0].value;
/*END OF THE DATE DIFFERENCE*/
/*START OF EVENT STATUS */
}
document.getElementById("days_to_go").innerHTML = event_status;
日期格式为 - &#39; YYYY,mm,dd&#39; 我已经尝试过&YYYY / mm / dd&#39;格式也。
但是在iOS的情况下,它始终会转到其他情况(var event_status =&#34;事件已过期&#34 ;;)。
我该如何解决这个问题?
答案 0 :(得分:1)
有4种方法可以启动日期:
new Date() new Date(milliseconds) new Date(dateString) new Date(year, month, day, hours, minutes, seconds, milliseconds)
你不能跟随他们中的任何一个。阅读the basics of Date object。