Convert Date: Need code

dvelloriy
Kilo Sage

Hello all,

 

I need to convert today’s date to Julian date. 
for ex: 13 sep 2024, in julian date it would be 24257.

then i need to append 0001 as suffix to this julian number.

final output = 242570001.

 

is it possible to run a loop and increment this number by 1 based on a count value provided in one of the catalog variables.

 

i am creating a flow. In the flow input would be the count variable from cat item. Then the script logic will calculate the numbers above in an array and then store them in an output variable on cat item.

 

 

 

please advise

8 REPLIES 8

Hi @dvelloriy 

I tested the below in background it should work :-


var today = new Date();


var startOfYear = new Date(today.getFullYear(), 0, 1);
var diff = today - startOfYear;
var dayOfYear = Math.floor(diff / (1000 * 60 * 60 * 24)) + 1;


var julianDate = today.getFullYear().toString().slice(2) + dayOfYear.toString().padStart(3, '0');


var baseNumber = parseInt(julianDate + '0001', 10);


var count = 5;
var numberArray = [];
for (var i = 0; i < count; i++) {
    numberArray.push(baseNumber + i);
}

gs.info('Generated numbers: ' + numberArray.join(', '));


--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

 YouTube: https://www.youtube.com/@learnservicenowwithravi
 LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

Hi @Ravi Gaurav , I was able to write the action and insert in my flow, its looking good.

Can we modify this script to include a validation step?

I need to check if these generated numbers (asset tags) are present in alm_hardware table or not.

If no, then we should continue with our logic, otherwise we need to skip the ones present and generate unique asset tags only. Please advise.

dvelloriy
Kilo Sage

Hi Ravi, This is awesome and works great.

Do you know how i can modify this logic now to include Count (input variable on cat item) and the array result should be displayed in output variable (on cat item).

i am planning to write a flow however if there is other easy way to do this, let me know plz.

Appreciate your help.

create custom action and pass your variable data as input into it and use script step..assign output

 

var count = ''; .// PASS YOUR INPUT ( variable data from mainFlow )
var numberArray = [];
for (var i = 0; i < count; i++) {
    numberArray.push(baseNumber + i);
}

 

output--> numberArray.join(', '));