Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cannot capture week if it falls between 2 months (last week of the month)

    My Problem is that I developed a code that can capture a week even if the days are from 2 different months. I have 3 input select namely Month, Period and Year.

    Here's how it works.

    Month is January to December
    Period is 1- 6 which represents weeks per month.
    <-----I'm still trying to develop how to make this dynamic in which it will show only number of weeks per month since what I did is I encoded the 1 to 6 within the code.
    Year which is auto generating.

    Scenario (this one works)
    If I choose August in Month and 1 in Period and 2014 in year then this will show (this is correct)

    August, Sun Jul 27 2014 00:00:00 GMT+0800 (China Standard Time) - 2 2014
    I'm trying to eliminate the SUN and the time along with the GMT+0800(China Standard Time) but that;s not the problem, just in case you know how kindly help me hehehe.

    Back to the main problem, But When I choose August in Month and 6 in period and 2014 in year what should be the output

    Output must be September , Sun Aug 31 2014 00:00:00 GMT+0800 (China Standard Time) - 6 2014 but instead of this it shows
    August, Sun Aug 31 2014 00:00:00 GMT+0800 (China Standard Time) - Mon Aug 25 2014 00:00:00 GMT+0800 (China Standard Time) 2014

    Here's the HTML for month period and year

    Code:
    <div id="downoption">
          <div class="input-group-addon" style="width:20%;" title='Filter By' id = "monthdiv">
          	<!--MONTH-->
    	       <label for="month" style="padding-right: 10px;margin-top:7px;">Month</label>
    		      <select name="month" id="month" class="text ui-widget-content ui-corner-all">
    		            <option value="0">Choose Month</option>
    		            <option value="January">January</option>
    		            <option value="February">February</option>
    		            <option value="March">March</option>
    		            <option value="April">April</option>
    		            <option value="May">May</option>
    		            <option value="June">June</option>
    		            <option value="July">July</option>
    		            <option value="August">August</option>
    		            <option value="September">September</option>
    		            <option value="October">October</option>
    		            <option value="Novermber">Novermber</option>
    		            <option value="December">December</option>
    		 	  </select>
     	  	<!--MONTH-->
     	  </div>
     	  <div class="input-group-addon" style="width:20%;" title='Filter By' id = "perioddiv">	
     	  	<!--PERIOD-->
    	       <label for="period" style="padding-right: 10px;margin-top:7px;" visible="false">Period</label>
    		      <select name="period" id="period" class="text ui-widget-content ui-corner-all">
    		            <option value="">Choose Period</option>
    		            <option value="1">1</option>
    		            <option value="2">2</option>
    		            <option value="3">3</option>
    		            <option value="4">4</option>
    		            <option value="5">5</option>
    		            <option value="6">6</option>
    		 	  </select>
     	  	<!--PERIOD-->
     	  </div>
     	  <div class="input-group-addon" style="width:20%;" title='Filter By' id="yeardiv">
     	  	<!--YEAR-->
    	       <label for="year" style="padding-right: 10px;margin-top:7px;">Year</label>
    		      <select name="year" id="year" class="text ui-widget-content ui-corner-all">
    		            <?php
    						$year = date("Y");
    						$start = $year - 100;
    						for ($i = $start; $start <= $year; $start++) {
    						    echo '<option value="' . $start. '">' . $start. '</option>';
    						}
    		            ?>
    		 	  </select>
    		</div> 
    		<br/>
    		<div class="input-group-addon" style="width:10%;" title='Filter By' id="yeardiv">
    	       <label for="year" style="padding-right: 10px;margin-top:0px;">Pay Period:</label><span id="dateStr"></span>
    		</div>  
    		</div>

    and here's the javascript

    Code:
    $(function(){
    		$("#period").change(function(){
    		      var dateStr = "";
    		      var month = $('#month').val();
    		      var monthToDisplay = $('#month option[value='+month+']').text();
    		      var period = $('#period').val();
    		      var year = $('#year').val();
    		      var date = new Date(month+'/01/'+year);
    		      var day = date.getDay();
    		      var diff = 7 - parseInt(day);
    		      var start = new Date(date.getTime() - 60*60*24* day*1000);
    				if (period > 1) {
    					start = new Date(start.getTime() + (60 * 60 * 24 * (7 * (period - 1)) * 1000));
    					}
    					end = new Date (start.getTime() - (60 * 60 * 24 * 6 * 1000));
    
    							var startMonthToDisplay = $('#month option[value='+start.getMonth()+']').text();
    							var endMonthToDisplay = $('#month option[value='+end.getMonth()+']').text();
    		      //var end = 31;
    		      //var lastDay = new Date(date.getFullYear(), date.getMonth(), 0).getDate();
    		      var lastDay = new Date(date.getTime() + 60 * 60 *24 * 6 * 1000);
    		      var lastDays = new Date(date.getTime() + 360 * 60 *24 * 6 * 1000);
    		        //alert(lastDay);
    		      if(period ==1)
    		      {
    		          end = diff;
    		          alert(start);
    		      }
    		      /*else if (period > 1 && period < 6)
    		      {
    		         start = diff + 7 * (parseInt(period) - 2)
    		         start = (parseInt(start + 1))
    		        end = start+6;
    		        if(parseInt(end) > parseInt(lastDay))
    		           end = lastDay;
    		      }
    					else if (period == 6)
    					{
    						lastDay = new Date(date.getTime() - 60 * 60 *24 * 6 * 1000);
    					start = diff + 7 * (parseInt(period) - 2)
    					end = start+6;
    					if(parseInt(end) > parseInt(lastDay))
    					end = lastDay;
    					};*/
    		       dateStr = monthToDisplay+', '+start+' - '+end+' '+year;
    		       // dateStr = start+' - '+monthToDisplay+' , '+end+' '+year;
    		      $('#dateStr').html(dateStr);
    		      
    		    });
    
    		    $("#year").change(function(){
    		      var dateStr = "";
    		      var month = $('#month').val();
    		      var monthToDisplay = $('#month option[value='+month+']').text();
    		      var period = $('#period').val();
    		      var year = $('#year').val();
    		      var date = new Date(month+'/01/'+year);
    		      var day = date.getDay();
    		      var diff = 7 - parseInt(day);
    		      var start = new Date(date.getTime() - 60*60*24* day*1000);
    		      if (period > 1) {
    					start = new Date(start.getTime() + (60 * 60 * 24 * (7 * (period - 1)) * 1000));
    					}
    					end = new Date (start.getTime() - (60 * 60 * 24 * 6 * 1000));
    
    		      //var end = 31;
    		      //var lastDay = new Date(date.getFullYear(), date.getMonth(), 0).getDate();
    		      var lastDay = new Date(date.getTime() + 60 * 60 *24 * 6 * 1000);
    		      var lastDays = new Date(date.getTime() + 360 * 60 *24 * 6 * 1000);
    		        //alert(lastDay);
    		      if(period ==1)
    		      {
    		          end = diff;
    		          alert(start);
    		      }
    		      /*else if (period > 1 && period < 6)
    		      {
    		         start = diff + 7 * (parseInt(period) - 2)
    		         start = (parseInt(start + 1))
    		        end = start+6;
    		        if(parseInt(end) > parseInt(lastDay))
    		           end = lastDay;
    		      }
    					else if (period == 6)
    					{
    						lastDay = new Date(date.getTime() - 60 * 60 *24 * 6 * 1000);
    					start = diff + 7 * (parseInt(period) - 2)
    					end = start+6;
    					if(parseInt(end) > parseInt(lastDay))
    					end = lastDay;
    					};*/
    		       dateStr = monthToDisplay+', '+start+' - '+end+' '+year;
    		       //dateStr = start+' - '+monthToDisplay+' , '+end+' '+year;
    		      $('#dateStr').html(dateStr);
    		    });
    
    		    $("#month").change(function(){
    		      var dateStr = "";
    		      var month = $('#month').val();
    		      var monthToDisplay = $('#month option[value='+month+']').text();
    		      var period = $('#period').val();
    		      var year = $('#year').val();
    		      var date = new Date(month+'/01/'+year);
    		      var day = date.getDay();
    		      var diff = 7 - parseInt(day);
    		      var start = new Date(date.getTime() - 60*60*24* day*1000);
    		      if (period > 1) {
    					start = new Date(start.getTime() + (60 * 60 * 24 * (7 * (period - 1)) * 1000));
    					}
    					end = new Date (start.getTime() - (60 * 60 * 24 * 6 * 1000));
    
    
    		      //var end = "";
    		      //var lastDay = new Date(date.getFullYear(), date.getMonth(), 0).getDate();
    		      var lastDay = new Date(date.getTime() + 60 * 60 *24 * 6 * 1000);
    		      var lastDays = new Date(date.getTime() + 360 * 60 *24 * 6 * 1000);
    		      if('#period option[value='+1+']')
    		      {
    		          end = diff;
    		          alert(start);
    		      }
    		      /*else if (period > 1 && period < 6)
    		      {
    		         start = diff + 7 * (parseInt(period) - 2)
    		         start = (parseInt(start + 1))
    		        end = start+6;
    		        if(parseInt(end) > parseInt(lastDay))
    		           end = lastDay;
    		      }
    					else if (period == 6)
    					{
    						lastDay = new Date(date.getTime() - 60 * 60 *24 * 6 * 1000);
    					start = diff + 7 * (parseInt(period) - 2)
    					end = start+6;
    					if(parseInt(end) > parseInt(lastDay))
    					end = lastDay;
    					};*/
    		       dateStr = monthToDisplay+', '+start+' - '+end+' '+year;
    		       //dateStr = start+' - '+monthToDisplay+' , '+end+' '+year;
    		      $('#dateStr').html(dateStr);
    		    });
    		});

    Need all the help I can get

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    This makes NO SENSE at all.
    Output must be September , Sun Aug 31 2014
    How can you possibly expect a data of August 31 to show as being in September????

    As for showing just the date, with no time no day, etc., etc.: This has been discussed in this forum many times before.

    One example:
    [code]
    var dt = new Date( ); // or however else you get a Date value
    var prettyDate = (dt.getMonth()+1) + "/" + dt.getDate() + "/" + dt.getFullYear();
    [/cod]e
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •