2014-03-26 162 views
0
<div id="cp_opening_hours"> 
<h3 class="howtoreach openinghrs">Opening Hours</h3> 
<span>Monday: 10.30 AM - 9.30 PM<br></span> 
<span>Tuesday: 10.30 AM - 9.30 PM<br></span> 
<span>Wednesday: 10.30 AM - 9.30 PM<br></span> 
<span>Thursday: 10.30 AM - 9.30 PM<br></span> 
<span>Friday: 10.30 AM - 9.30 PM<br></span> 
<span>Saturday: 10.30 AM - 9.30 PM<br></span> 
<span>Sunday: 10.30 AM - 9.30 PM<br></span></div> 

....顯示日期和時間

var dayNames = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'); 
var nData = new Date(); 

     jQuery("#cp_opening_hours span").each(function(i) { 

     var to_day=jQuery(this).text(); 

     to_day_new=to_day.split(" "); 

     to_day_new1=to_day_new[0].split(':'); 


     span_pos=dayNames.lastIndexOf(dayNames[nData.getDay()]); 

     span_val=jQuery("#cp_opening_hours span:nth-of-type("+span_pos+")").text(); 

     if(span_val!=undefined){jQuery("#thelanguage_new").append('<span>'+span_val+'</span>');} 
     jQuery("#cp_opening_hours span:nth-of-type("+span_pos+")").remove(); 


     if(to_day_new1[0]=='Sunday') 
     { 

      jQuery(this).remove(); 
      to_day_new_time=to_day.split(':'); 

      main_today_time=to_day_new_time[1]; 

      if(main_today_time=="Closed") 
      { 
      jQuery('#cp_opening_hours div').prepend('<span class="closed">Today: '+main_today_time+'<br/></span>'); 

      } 
      else 
      { 
       jQuery('#cp_opening_hours div').prepend('<span class="open">Open Today: '+main_today_time+'<br/></span>'); 
      } 


     } 


    }); 
    jQuery("#thelanguage_new p:nth-of-type(1)").remove(); 

我想告訴今天基地的日期和時間,如果今天是星期三。所以我想這樣的

<div id="cp_opening_hours"> 
<h3 class="howtoreach openinghrs">Opening Hours</h3> 

<span>Open Today: 10.30 AM - 9.30 PM<br></span> 
<span>Thursday: 10.30 AM - 9.30 PM<br></span> 
<span>Friday: 10.30 AM - 9.30 PM<br></span> 
<span>Saturday: 10.30 AM - 9.30 PM<br></span> 
<span>Sunday: 10.30 AM - 9.30 PM<br></span> 
<span>Monday: 10.30 AM - 9.30 PM<br></span> 
<span>Tuesday: 10.30 AM - 9.30 PM<br></span></div> 

回答

0

也許你想這個操縱 - 我可以給你一個更優雅的解決方案後

Live Demo

var dayNames = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; 
var today = new Date(); 
var fromToday = dayNames.slice(today.getDay()); 
var endDates = dayNames.slice(0,today.getDay()); 
var dayNamesSorted = fromToday.concat(endDates); 
var $div = $("#cp_opening_hours"); 
$div.html('<h3 class="howtoreach openinghrs">Opening Hours</h3>'); 
$.each(dayNamesSorted,function(i,day) { 
    $div.append('<span>'+(i===0?"Today":day)+' '+': 10.30 AM - 9.30 PM</span><br/>'); 
}); 
+0

非常感謝你 – user3405206

+0

不客氣。如果有用,你可以[接受答案](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – mplungjan