2015-04-15 47 views
1

我使用Date()函數獲取當前日期並將其與另一日期進行比較。Javascript Date()函數在Safari(iOS)中不起作用

它與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; 

和日期格式是 - 'YYYY,MM,DD' 和我已經試過 'YYYY/MM/DD' 的格式也。

但是在iOS的情況下總是會出現其他情況(var event_status =「Event has expired」;)。

我該如何解決這個問題?

回答

1

有4種方式啓動日期:

new Date() 
new Date(milliseconds) 
new Date(dateString) 
new Date(year, month, day, hours, minutes, seconds, milliseconds) 

你不遵循任何人。閱讀the basics of Date object

+1

OP的日期格式實際上在Javascript中是有效的日期格式,Safari中存在問題,它會返回無效的日期,其他瀏覽器將接受發佈的dateStrings – AaronH

相關問題