Jscript on date only field

Hi All,

Hoping someone can help me, a bit rusty on my java
I have a custom field comp_c_enddate, this field is set as date only

The default value for this field sets it at current date plus 1 year
but what i need to do is onchange event of this field I want to make sure that whatever day/month is selected that it will choose the last day of that month

So for example if I choose 20/03/2013, when I leave or tab away from the field it will default to 31/03/2013

Any advice appreciated
Thanks,

Paul

  • 0

    Hi Paul,

    I'm not sure this is necessarily the most efficient way of doing it, but it works for me!

    //get the selected date and parse it
    var dString = this.value;
    var year = dString.substring(6);
    var yearInt = parseInt(year);
    var day = dString.substring(0,3);
    var month = dString.substring(3,6);
    var monthInt = parseInt(month);

    //put in date format, add a month and then go back a day
    int_d = new Date(yearInt, monthInt, 1);
    d = new Date(int_d - 1);

    //This gives you the last day of the current month
    var newDay = d.getDate();

    //Then put the day value back together with the month and year and set your field value
    var newDate = newDay + '/' + month + year;
    comp_c_enddate.value = newDate;

    Hope it helps.

    Pete.

  • 0

    Hi Pete,

    Spot on, thanks a million, that works a treat.

    Hopefully I should be able to utilize and manipulate this and use it to create a date based on another date field

    Thanks again

    Paul