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



Last day of previous month Javascript

asked on August 3, 2018

Hey everyone,

Does anyone have a simple JavaScript solution for a calendar field in my table? I just want that field to default to the last day of the previous month (whatever that may be). Thanks!

0 0


replied on August 8, 2018

I broke it out a little bit and it would be formatted for Forms.  If you submit a form with the time.h format it will most likely fail.


$(document).ready(function () {
    var dateObj = new Date();

    var month = dateObj.getMonth() + 1;
    var day = dateObj.getDate();
    var year = dateObj.getFullYear();

    newdate = month + "/" + day + "/" + year;
    $('#q3 input').val(newdate);

Where 'q3' is your Date field.  You should get something like:

1 0
replied on August 10, 2018 Show version history

Awesome, thank you so much!

I just tried out this solution and didn't have much luck with it. It could very well be something I'm doing wrong in moving over your code to this test form though. I attempted to upload the test form here, but unfortunately it looks like it can't be done, so instead, here's a screenshot of it.

In it, "Column 2" has been made a date field, and it's basically a blank template other than the JS code you had me add. The field doesn't auto-populate when the form loads (the goal), and doesn't appear to change the date field. Again, it's most likely I'm doing something wrong here, but if you have a chance to look at it, I'd appreciate it! We have a temp solution in place in the meantime for the actual live form.

0 0
replied on August 10, 2018

There is a semi-colon missing after the dateObj.setDate(0) statement.

While in the javascript tab of Forms you can press F12 and choose the console tab to see if there is any errors after you click the Save button. This helps when your javascript appears to not be doing anything at all.

1 0
replied on August 6, 2018
  var today = new Date();
  var endOfLastMonth = new Date(today.getYear(),today.getMonth(),0)

Setting the day to 0 returns the last day of the month.

For some reason the getMonth method returns the number of the month starting with 1 but the Date function expects the number of the month starting with 0. So when you input the getMonth result you automatically get last month.

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

Sign in to reply to this post.