You are viewing limited content. For full access, please sign in.

Question

Question

Calculation Being Done on Current Date not Chosen Date

asked on October 30, 2017

Hello All,

 

I am utilizing moment.js to do take care of some times and dates in a time sheet form.  I want to automatically calculate the dates for a full week based upon input from the user.  I have the following to preform the calculations.  It works accept for one thing:  it uses today's date instead of the date the user selects which is  .weekEndingDate  I believe the issue lies in the way I am declaring and assigning the value to lastDayOfWeek.  If it helps, the date will print out in the following format if no formatting is applied to it: Mon Oct 30 2017 15:47:55 GMT-0400   Any suggestions would be greatly appreciated.  

function findDates() { 
      var calcDay;
      var formatDate;
                  
      lastDayOfWeek = moment($(this).find('.weekEndingDate input').val()); // get last day of week and set as moment item 		
      formatDate = lastDayOfWeek.format('MM' + '/'+ 'DD' + '/'+ 'YYYY');	// format date for display
      $('.weeklyTimesTable tbody tr:nth-child(' + 7 +')').find('.date input').val(lastDayOfWeek);      
      do {
        	calcDay = lastDayOfWeek.subtract(1, 'days'); // calculate days in decsending order
        	formatDate = calcDay.format('MM' + '/'+ 'DD' + '/'+ 'YYYY'); // format date for display
        	$('.weeklyTimesTable tbody tr:nth-child(' + weekDayCount +')').find('.date input').val(formatDate);
        	weekDayCount--; 
      } while (weekDayCount >= 1);
           	  
}  //end findDates 

 

0 0

Replies

replied on October 30, 2017

Hello,

lastDayOfWeek = moment($(this).find('.weekEndingDate input').val());

Here, I think "$(this).find('.weekEndingDate input').val()" returns "undefined"; which will set "lastDayOfWeek" as today's date. Probably, it's because your selector cannot correctly find the input box. The most common cause for such issue is typo in either selector or class name in html. Once your selector can correctly find the input, "lastDayOfWeek" will be correctly updated as the user's value from the input box. 

Thank you

0 0
replied on October 31, 2017

Good Morning Yoonsang,

I am thinking that it is possibly because I am using the class for the entire calendar/date picker field, instead of just an identifier for the input field or something similar.  But I am having a hard time finding the correct identifier to utilize for the selector to correctly locate the input.  I pulled all relevant scripting for the date picker via the Identifier utility in my browser.  (I removed information about the label and error fields.)  Any help would be greatly appreciated since I am still quite new to javascript

 

<li attr="Week_Ending_Date" attrtype="date" name="q97" id="q97" class=".weekEndingDate form-q label-left form-focused">

<div class="cf-field"><div class="cf-date-field-date">

<input type="text" id="Field97" name="Field97" aria-labelledby="Field97" class="cf-small calendar cf-medium bfOnKeyup parsley-success hasDatepicker" required="True" min="1917-01-01" max="2117-12-31" momentpattern="MM/DD/YYYY" vo="e" data-parsley-valid-format="date" data-parsley-id="19”>

0 0
replied on October 31, 2017

Hello,

If you are using the class for the entire calendar/date picker fields, using the class might not be the best option for you. Fortunately, we have Id value for the input we want to select. Unlike 'class', Id's are unique values. You will be able to get the value by using Id selector. The following code is an example of Id selector. 

lastDayOfWeek = moment($('#Field97').val());

Thank you

0 0
replied on October 31, 2017

That worked!  Thank you very much, YoonSang!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.