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

Question

Question

Force the Date Field to the 15th of the month

asked on August 23, 2017 Show version history

I have a Form that requires a staff member to select the Month and Year that they want their Payslip from. The day, however, must be the 15th. I have some Javascript that changes the Day to the 15th.

However, it only seems to work if the day selected is greater than the 12th.

 

$(function() {
  $(document).ready(function () {
    
    $('.15Date input').on('change', function() {
      var selectedDate = new Date($('.15Date input').val());
      var year = selectedDate.getFullYear();
      var month = ("0" + (selectedDate.getMonth() + 1)).slice(-2);
      var newVal = '15/' + month + '/' + year;
      $('.15Date input').val(month + '/15/' + '/' + year);
    });
 
  });
});

Any help will be appreciated

0 0

Answer

SELECTED ANSWER
replied on August 24, 2017

Hey im not 100% sure where the issue lies with your script. im guessing its with the date formating tho.

I like to use moment for handling dates, try this.

$('.15Date input').on('change', function() {
      var dateCalendarPart = moment($('.15Date input').val(),'DD/MM/YYYY');
      var year = dateCalendarPart.year();
      var month = dateCalendarPart.month();
      $('.15Date input').val(moment().year(year).month(month).date(15).format('DD/MM/YYYY'));
    });

You may need to swap your format strings to 'MM/DD/YYYY'  if you use American dates.

 

Hope this helps :)

1 0

Replies

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

Sign in to reply to this post.