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

Question

Question

default date to beginning of month

asked on October 4, 2017

I have a timesheet where the user selects the start date for the time period they are submitting.  If the user accidently selects a different date (say...Jan 2, instead of Jan 1), I would like the date to default to the first of that month.

I could not figure out how to set a default that would work in this manner.

I was hoping to achieve this through formulas, but have not had any luck.  Any ideas?  

I am not an accomplished at coding.  If the answer falls to JavaScript, please be detailed.

 

0 0

Answer

SELECTED ANSWER
replied on October 4, 2017

Hey,

This is similar to another question i've answered in the past, hopefully this will work for you as well.

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

Just add the class FirstDate to the date field and insert this into your javascript section.

1 0
replied on October 5, 2017

Worked!

Thanks.

0 0

Replies

replied on October 4, 2017 Show version history

An option would be to simply create a drop-down menu with the 12 dates; from Jan-Dec. since it doesn't seem that you will be using the rest of the dates.

If you must have the calendar, this works really nice.

Plug this formula in another textbox and give the first calendar with a Date variable.

=EOMONTH(Date,-1)+1

And finally if you want to hide the second box and just updates the first box when you click out of it, this could work.

A little creative there. I know others know of a better way. 

The code below assumes that your first box has a class of "Date" and your second one has a class of "First_day_of_month"

$( document ).ready(function() {
  
      $(document).on('change', '.Date input', relect);
  
  $('.First_day_of_month').hide();
  
  function relect(){

    var First_day_of_month = $('.First_day_of_month input').val();
    $('.Date input').val(First_day_of_month);
  }
  
});
DatePicker.PNG
DatePicker.PNG (7.71 KB)
0 0
replied on October 4, 2017

That would work, but I would like to avoid having to update it on a yearly basis.  

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

Sign in to reply to this post.