Default value to be show as 2 years from current date in DD/MM/YYYY format ;while creating new record in table

Abhradipa Baner
Kilo Contributor

I want to set Default value of "Expiry Date " field is +2 years from current date in DD-MM-YYYY format. 

I have given Type of field as DATE for "Expiry Date " and written the below OnLoad() Client Script -

function onLoad() {
//Type appropriate comment here, and begin script below
if(g_form.isNewRecord()){
var todayDate = new Date(new Date().setFullYear(new Date().getFullYear() + 2)); // Now
g_form.setValue('expiry_date', todayDate.toISOString().slice(0,10));
}
}

// Output : Invalid Date while submitting new request.

Then I got to know "Date" type ideally took YYYY-MM-DD format ;So it is causing error as an Invalid Date. 

But I want to set it by default in DD-MM-YYYY format as this is our company's default format . 

How I can fix this issue? Please guide .

Regards,

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

the date format would be shown as per logged in user's date format

You need not worry on that.

Let the system set the date in system format yyyy-mm-DD and system will take care of other formats

you can use default value in that date field

javascript: var dt; var gdt = new GlideDateTime(); gdt.addYearsLocalTime(2); dt = gdt.getLocalDate(); dt;

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

the date format would be shown as per logged in user's date format

You need not worry on that.

Let the system set the date in system format yyyy-mm-DD and system will take care of other formats

you can use default value in that date field

javascript: var dt; var gdt = new GlideDateTime(); gdt.addYearsLocalTime(2); dt = gdt.getLocalDate(); dt;

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur,

Thanks for your suggestion. Yes it's working for me now. 

Regards,

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi  Abhradipa,

Ankur's reply on setting default value should work but in case it's required to use client script, following client script will set the default date that is 2 years from the current date in field "expiry_date".

function onLoad() {
    try {
        var today = new Date();
        today.setFullYear(today.getFullYear() + 2);
		var dateFormat = g_user_date_format;  // get end user's date format
		dateFormat = dateFormat.replace('yyyy', today.getFullYear());
		dateFormat = dateFormat.replace('MM', padZero(today.getMonth()+1));
		dateFormat = dateFormat.replace('dd', padZero(today.getDate()));
        g_form.setValue('expiry_date', dateFormat);
    } catch (e) {
        alert(e.message);
    }

    function padZero(str) {
        var s = ('00' + str).slice(-2);
        return s;
    }
}

Execution output:

find_real_file.png

Hi Hitoshi,

I had tried with the default value separately as well as with the Client Script. Both approaches are working for me.

Thanks for your suggestion.

Regards,