Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

converting the current date into logged in user's format in a Business rule

Community Alums
Not applicable

Hi all,
I have a requirement to get the current date, add a given no of days to that date and set the date to a date field in logged in user's format in a Business Rule. How can I achieve it?

Thanks in advance

1 ACCEPTED SOLUTION

Amitoj Wadhera
Kilo Sage

Hello @Community Alums ,

 

Here is the code for your requirement:

    var currentDate = new GlideDateTime();
    
    function addDays(dateTime, days) {
        var gdt = new GlideDateTime(dateTime);
        gdt.addDaysUTC(days);
        return gdt;
    }

    var daysToAdd = 10;
    var newDate = addDays(currentDate, daysToAdd);

    var userDateFormat = gs.getProperty('glide.sys.date_format');
    var formattedDateString = newDate.getLocalDate().getDisplayValueInternal();

    current.your_date_field = formattedDateString;
    
    gs.info("Formatted Date: " + formattedDateString);

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera

View solution in original post

1 REPLY 1

Amitoj Wadhera
Kilo Sage

Hello @Community Alums ,

 

Here is the code for your requirement:

    var currentDate = new GlideDateTime();
    
    function addDays(dateTime, days) {
        var gdt = new GlideDateTime(dateTime);
        gdt.addDaysUTC(days);
        return gdt;
    }

    var daysToAdd = 10;
    var newDate = addDays(currentDate, daysToAdd);

    var userDateFormat = gs.getProperty('glide.sys.date_format');
    var formattedDateString = newDate.getLocalDate().getDisplayValueInternal();

    current.your_date_field = formattedDateString;
    
    gs.info("Formatted Date: " + formattedDateString);

 

If you find my response helpful, please consider marking it as the 'Accepted Solution' and giving it a 'Helpful' rating. Your feedback not only supports the community but also encourages me to continue providing valuable assistance.

 

Thanks,

Amitoj Wadhera